AutoCAD PID component coding automation with LSP and DCL

AutoCAD PID component coding automation with LSP and DCL

wouter_klootwijk
Explorer Explorer
1,042 Views
4 Replies
Message 1 of 5

AutoCAD PID component coding automation with LSP and DCL

wouter_klootwijk
Explorer
Explorer

Hello! I am working on a code to insert a block into my drawing. When the user uses the command “ETP_PID,” a dialog appears. In this dialog, the user can select a name of a DWG (first column in DCL code). When the user clicks on “OK,” the selected block should be inserted.

 

However, my code only works when the block has already been inserted in the drawing previously. My wish is that the code looks up the block name in my “support file search paths.”

 

Can anyone help? I hope my problem/question isn't too specific.

0 Likes
Accepted solutions (1)
1,043 Views
4 Replies
Replies (4)
Message 2 of 5

MrJSmith
Advocate
Advocate

Your problem is you are using vla command to insert the block. VLA command is only referencing the current document object as seen here "(vla-insertblock (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))".

 

There are two options: 

  1. "Steal" the block first from the drawing it resides in. Lee Mac has a very good function to use for this task; however, you have to explicitly pass it the drawing the block resides in which then wouldn't use the support paths to look for the block. https://www.lee-mac.com/steal.html
  2. Another option is to use the autocad command "insert" which will trigger a search for the block based on support paths. Here is my "vlaMakeBlock" function that does just that.

 

(defun vlaMakeBlock (blockName layerName pt DS Rotation / blk) ;entMake a block with its name, layer, location, Rotation (rads), scale.
	(if (not (tblobjname "block" (cadr (fnsplitl blockName))))
		(progn
			(notify "Making Block")
			(layerExists layerName)
			(command "-insert" blockName 1 1 1 0)
			(entdel (entlast))
		)
	)
	(layerExists layerName)
	(setq blk (vla-insertblock (vlax-get-property *acdoc* (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace)) (vlax-3D-point pt) blockName DS DS 1.0 rotation))
	(vla-put-layer blk layerName)
	blk

)

 

Basically you just need call "(command "-insert" blockName 1 1 1 0)" then delete it with "(entdel (entlast))" and then you can use your vla command to insert it as the block reference would then be in the document.

 

0 Likes
Message 3 of 5

paullimapa
Mentor
Mentor
Accepted solution

I add this function to check for block if in dwg or can be located in support path for insert:

; function to locate block
; returns nil if not found
(defun findblk (nam-arg / attreq flg)
 (if(not (setq flg (tblsearch "BLOCK" nam-arg))) ; look in block table
   (if(setq flg (findfile (strcat nam-arg ".dwg"))) ; look in support path
    (progn ; found
      (setq attreq (getvar"attreq")) ; save setting
      (setvar "attreq" 0) ; disable attribute dialog
      (vl-cmdf"_.Insert" flg "0,0,0" "" "" "")
      (entdel (entlast)) ; delete the insert
      (setvar "attreq" attreq) ; restore setting
    ) ; progn
   ) ; if
 ) ; if
 flg
)

Then include the function to check for the block here:

  ; Check of ETP_PID_LST3 is ingesteld en voer de blokinvoeging uit als dat zo is
  (if (and ETP_PID_LST3 (not (equal ETP_PID_LST3 "0")))
      (progn
; check if block is in dwg or can be inserted
(if (findblk (strcat "ETP-PID-" (nth (atoi LST3) LIJST3)))
 (progn
	(vla-insertblock (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
		 		 (vlax-3d-point (getpoint "\Pick point to insert block: "))
		 		 (strcat "ETP-PID-" (nth (atoi LST3) LIJST3)) 
		 		 1 1 1 0
	)
	(setpropertyvalue (entlast) "Code" (strcat (nth (atoi LST1) LIJST1) "" (nth (atoi LST2) LIJST2) "" (nth (atoi LST5) LIJST5) "" (nth (atoi LST4) LIJST4)))
        (princ "\nBlock inserted.")
 ) ; progn
 (princ (strcat "\nBlock: [ETP-PID-" (nth (atoi LST3) LIJST3) "] Not Found in current drawing or in AutoCAD support path."))
)  ; if
;
      )
  ) ; if

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 4 of 5

wouter_klootwijk
Explorer
Explorer
Thanks! This works for me.
0 Likes
Message 5 of 5

paullimapa
Mentor
Mentor

Glad to have helped…cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes