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.
Solved! Go to Solution.
Solved by paullimapa. Go to Solution.
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:
(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.
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
Glad to have helped…cheers!!!
Can't find what you're looking for? Ask the community or share your knowledge.