Jump to content

Recommended Posts

Posted (edited)

Please, someone help me. I thought my program was in its final stages, but it is not. I used AutoCAD 2002 and 2007 during development, but when I tried to install it in AutoCAD 2021, this happened (see the picture). It prompts as if I need to edit the attributes one by one. Also, how can I eliminate the automatic prompts in the command line made by AutoCAD, while keeping the programmed prompts within the AutoLISP?

 

(defun c:nancsv ( / fname dummy pts newline blockName height x y base-point auto-numbering num)

  (princ "\nImport CSV/Text File by: Nan Glase (2025-01-04)")
;  (princ "\nEnter \"NANCSV\" to run.")

  ; 
  (defun csv->lst ( str / pos )
    (if (setq pos (vl-string-position 44 str))  ; 
        (cons (substr str 1 pos) (csv->lst (substr str (+ pos 2))))  ; 
        (list str)
    )
  )

  ; Set the block name
  (setq blockName "Nan Glase")  

  ; Prompt for text height
  (setq height (getreal "\nEnter text height [0.60]: ")) ; Prompt for text height

  ; If the user presses Enter without input (height is nil), set the default value to 0.60
  (if (not height) 
    (setq height 0.60)
  )

  ; Apply the formula (value entered / 2)
  (setq height (/ height 2)) ; Divide the height by 2

  ; Define acceptable inputs for Yes and No
  (setq yes-options '("" "Y" "y" "Yes" "yes" "YES"))
  (setq no-options '("N" "n" "No" "no" "NO"))

  ; Prompt for automatic point numbering in a loop until valid input is given
  (setq auto-numbering nil)
  (while (not (or (member auto-numbering yes-options)
                  (member auto-numbering no-options)))
    (setq auto-numbering (getstring "\nAutomatic point numbering? (Yes/No): "))
  )

  ; Start numbering from 1 if automatic numbering is enabled
  (if (member auto-numbering yes-options)
    (setq num 1)
    (setq num nil)  ; Set num to nil if automatic numbering is not enabled
  )

  ; Open the CSV file
  (setq fname (open (getfiled "Import CSV/Text File" "d:\\" "csv;txt" 16) "R"))
  (setq dummy (read-line fname)) ; 

  ; 
  (while (setq newline (read-line fname))
    (setq pts (csv->lst newline))  ; 

    ; If automatic numbering is enabled, modify the first element in pts
    (if num
      (setq pts (cons (itoa num) (cdr pts)))  ; 
    )

    ; Extract coordinates from the list
    (setq x (atof (nth 2 pts)))    ; X coordinate from CSV
    (setq y (atof (nth 1 pts)))    ; Y coordinate from CSV
    (setq base-point (list x y))   ; Create the base point list

    ; Insert the block reference with the specified base point
    (command "-insert" blockName   ; 
             base-point           ; Use the base point defined above
             height               ; Scale X (use the calculated height)
             height               ; Scale Y (use the calculated height)
             0                    ; Rotation
             (nth 0 pts)          ; column A from CSV
             (last pts)           ; column E from CSV
             (nth 3 pts)          ; column D from CSV
    )

    ; Increment the number for the next point if automatic numbering is enabled
    (if num
      (setq num (1+ num))
    )
  )

  ; Close the file after processing
  (close fname)  ; Close the file

  ; Notify user that the process is complete
  (princ "\nProcess completed successfully!") ; Completion message

  (princ) ; End of the function
)

;  (princ "\nImport CSV/Text File by: Nan Glase (2025-01-04)")
  (princ "\nEnter \"NANCSV\" to run Import CSV/Text File.")

 

error.JPG

Nan Glase.dwg

csv.JPG

Edited by NanGlase
additional photo (csv)
Posted (edited)

classic case of setvar "ATTREQ" 0 at begin of routine? Same for setvar "CMDECHO"

Edited by rlx
  • Like 1
Posted (edited)

A few comments;

 

Dont need CSV can read excel or Libre calc directly, search here for "Alan excel.lsp" it has lots of functions that you can use. Happy to discuss further.

 

Yes or No, there is a Acet function "yes no", do a google, or you can use this radio buttons program which can be used in any code, there is a yes or no example at the top of the code. The other is to use a INITGET method. Multi radio buttons.lsp 

 

Ps why not add a point number in excel so easy to just do 1 & 2 column "A" then drag down to last cell. A Ps ps the code can update your excel re a point number adding to column "A".

 

 

Edited by BIGAL
  • Like 1
Posted
2 hours ago, BIGAL said:

A few comments;

 

Dont need CSV can read excel or Libre calc directly, search here for "Alan excel.lsp" it has lots of functions that you can use. Happy to discuss further.

 

Yes or No, there is a Acet function "yes no", do a google, or you can use this radio buttons program which can be used in any code, there is a yes or no example at the top of the code. The other is to use a INITGET method. Multi radio buttons.lsp 

 

Ps why not add a point number in excel so easy to just do 1 & 2 column "A" then drag down to last cell. A Ps ps the code can update your excel re a point number adding to column "A".

 

 

Some of us use an RTK controller that exports survey data in a default CSV format without point numbering in column A, and others prefer to import it into AutoCAD with fewer attributes for a clearer drawing window. So I added an option for automatic numbering or not.

Posted (edited)

Don't use '(command "-insert"... '
Use the function I attached instead.

 

Of course: this function assumes that the invoked block is already defined by the user and contains the attributes specified in the argument list.


This, i think, will solve all your problems

 

(defun insertBlk (nmBlk east north npt elev descr / fijAtributos insertaBlqAtrbs)
  (defun fijAtributos (vlaEntBlq     nPto	   cota
		       descripcion   /		   attr-list
		       attr-postion  Attr_Object   lisAtributos
		      )
    (setq lisAtributos
	   (safearray-value
	     (variant-value
	       (vla-getattributes
		 vlaEntBlq
	       )
	     )
	   )
    )
    (VLAX-PUT-PROPERTY (nth 0 lisAtributos) "TextString" nPto)
    (VLAX-PUT-PROPERTY (nth 1 lisAtributos) "TextString" cota)
    (VLAX-PUT-PROPERTY
      (nth 2 lisAtributos)
      "TextString"
      descripcion
    )
  )

  (defun insertaBlqAtrbs (nombre pt_ins)
    (vla-InsertBlock
      (vla-get-modelspace
	(vla-get-activedocument
	  (vlax-get-acad-object)
	)
      )
      (vlax-3d-point pt_ins)
      nombre
      1
      1
      1
      0
    )
  )
  (setq vlaEntBlq (insertaBlqAtrbs nmBlk (list east north)))
  (fijAtributos vlaEntBlq npt elev descr)
)

;;;(insertBlk "Nan Glase" 400000 4500000 "1" "333.33" "My description")

 

 

Edited by GLAVCVS
  • Like 1
Posted
6 hours ago, GLAVCVS said:

Don't use '(command "-insert"... '
Use the function I attached instead.

 

Of course: this function assumes that the invoked block is already defined by the user and contains the attributes specified in the argument list.


This, i think, will solve all your problems

 

(defun insertBlk (nmBlk east north npt elev descr / fijAtributos insertaBlqAtrbs)
  (defun fijAtributos (vlaEntBlq     nPto	   cota
		       descripcion   /		   attr-list
		       attr-postion  Attr_Object   lisAtributos
		      )
    (setq lisAtributos
	   (safearray-value
	     (variant-value
	       (vla-getattributes
		 vlaEntBlq
	       )
	     )
	   )
    )
    (VLAX-PUT-PROPERTY (nth 0 lisAtributos) "TextString" nPto)
    (VLAX-PUT-PROPERTY (nth 1 lisAtributos) "TextString" cota)
    (VLAX-PUT-PROPERTY
      (nth 2 lisAtributos)
      "TextString"
      descripcion
    )
  )

  (defun insertaBlqAtrbs (nombre pt_ins)
    (vla-InsertBlock
      (vla-get-modelspace
	(vla-get-activedocument
	  (vlax-get-acad-object)
	)
      )
      (vlax-3d-point pt_ins)
      nombre
      1
      1
      1
      0
    )
  )
  (setq vlaEntBlq (insertaBlqAtrbs nmBlk (list east north)))
  (fijAtributos vlaEntBlq npt elev descr)
)

;;;(insertBlk "Nan Glase" 400000 4500000 "1" "333.33" "My description")

 

 

how to replace "-insert" function with that code?

Posted (edited)
(defun c:nancsv	(/	    fname      dummy	  pts	     newline
		 blockName  height     x	  y	     base-point
		 auto-numbering	       num	  csv->lst   insertBlk
		)

  (princ "\nImport CSV/Text File by: Nan Glase (2025-01-04)")
					;  (princ "\nEnter \"NANCSV\" to run.")

					;
  (defun csv->lst (str / pos)
    (if	(setq pos (vl-string-position 44 str)) ;
      (cons (substr str 1 pos) (csv->lst (substr str (+ pos 2)))) ;
      (list str)
    )
  )

  (defun insertBlk (nmBlk	east	    north	npt
		    elev	descr	    /		fijAtributos
		    insertaBlqAtrbs
		   )
    (defun fijAtributos	(vlaEntBlq     nPto	     cota
			 descripcion   /	     attr-list
			 attr-postion  Attr_Object   lisAtributos
			)
      (setq lisAtributos
	     (safearray-value
	       (variant-value
		 (vla-getattributes
		   vlaEntBlq
		 )
	       )
	     )
      )
      (VLAX-PUT-PROPERTY (nth 0 lisAtributos) "TextString" nPto)
      (VLAX-PUT-PROPERTY (nth 1 lisAtributos) "TextString" descripcion)
      (VLAX-PUT-PROPERTY
	(nth 2 lisAtributos)
	"TextString"
	cota
      )
    )

    (defun insertaBlqAtrbs (nombre pt_ins)
      (vla-InsertBlock
	(vla-get-modelspace
	  (vla-get-activedocument
	    (vlax-get-acad-object)
	  )
	)
	(vlax-3d-point pt_ins)
	nombre
	1
	1
	1
	0
      )
    )
    (if (tblsearch "block" nmBlk)
      (progn
        (setq vlaEntBlq (insertaBlqAtrbs nmBlk (list east north)))
        (fijAtributos vlaEntBlq npt elev descr)
      )
      (progn
        (alert (strcat "ERROR: Block \'" nmBlk "\' is NOT DEFINED"))
	(if fname (close fname))
	(exit)
      )
    )
  )


					; Set the block name
  (setq blockName "Nan Glase")

					; Prompt for text height
  (setq height (getreal "\nEnter text height [0.60]: "))
					; Prompt for text height

					; If the user presses Enter without input (height is nil), set the default value to 0.60
  (if (not height)
    (setq height 0.60)
  )

					; Apply the formula (value entered / 2)
  (setq height (/ height 2))		; Divide the height by 2

					; Define acceptable inputs for Yes and No
  (setq yes-options '("" "Y" "y" "Yes" "yes" "YES"))
  (setq no-options '("N" "n" "No" "no" "NO"))

					; Prompt for automatic point numbering in a loop until valid input is given
  (setq auto-numbering nil)
  (while (not (or (member auto-numbering yes-options)
		  (member auto-numbering no-options)
	      )
	 )
    (setq auto-numbering
	   (getstring "\nAutomatic point numbering? (Yes/No): "
	   )
    )
  )

					; Start numbering from 1 if automatic numbering is enabled
  (if (member auto-numbering yes-options)
    (setq num 1)
    (setq num nil)			; Set num to nil if automatic numbering is not enabled
  )

					; Open the CSV file
  (setq
    fname (open	(getfiled "Import CSV/Text File" "d:\\" "csv;txt" 16)
		"R"
	  )
  )
  (setq dummy (read-line fname))	;

					;
  (while (setq newline (read-line fname))
    (setq pts (csv->lst newline))	;

					; If automatic numbering is enabled, modify the first element in pts
    (if	num
      (setq pts (cons (itoa num) (cdr pts))) ;
    )

					; Extract coordinates from the list
    (setq x (atof (nth 2 pts)))		; X coordinate from CSV
    (setq y (atof (nth 1 pts)))		; Y coordinate from CSV
    (setq base-point (list x y))	; Create the base point list

					; Insert the block reference with the specified base point
;;;    (command "-insert"
;;;	     blockName			;
;;;	     base-point			; Use the base point defined above
;;;	     height			; Scale X (use the calculated height)
;;;	     height			; Scale Y (use the calculated height)
;;;	     0				; Rotation
;;;	     (nth 0 pts)		; column A from CSV
;;;	     (last pts)			; column E from CSV
;;;	     (nth 3 pts)		; column D from CSV
;;;    )
    (insertBlk blockName x y (if num (itoa num) "") (nth 3 pts) (last pts))

					; Increment the number for the next point if automatic numbering is enabled
    (if	num
      (setq num (1+ num))
    )
  )

					; Close the file after processing
  (close fname)				; Close the file

					; Notify user that the process is complete
  (princ "\nProcess completed successfully!") ; Completion message

  (princ)				; End of the function
)

					;  (princ "\nImport CSV/Text File by: Nan Glase (2025-01-04)")
(princ "\nEnter \"NANCSV\" to run Import CSV/Text File.")

 

Edited by GLAVCVS

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...