Extract dynamic block from parent drawing, insert in new drawing

Extract dynamic block from parent drawing, insert in new drawing

Anonymous
Not applicable
2,031 Views
10 Replies
Message 1 of 11

Extract dynamic block from parent drawing, insert in new drawing

Anonymous
Not applicable

I have a parent drawing with all the dynamic blocks we are using.

I have a lisp routine that will insert a toilet, and then after a few more clicks/answers, you are asked what the vent size is to be. Here you answer 1.5", 2", or 3". This determines the block name to insert. Ex. I type 1.5, it will insert "DWV Fittings-1.5" displaying the default visibility state.

 

What I need to do is, if the blocks do not exist in the drawing, the routine will go out and look for the "Dynamic Blocks.dwg" within my support search paths, then extract the correct dynamic block from this drawing.

 

Is this possible?

 

Below is the lisp routine (note this routine is associated with a tool palette command):

 

 

(defun WC (B / P1 P2 P3 P4 P5 P6 A1 A2 STYLE VENT)
(setq ERRORSAVE *ERROR*)
(setq OS (getvar "OSMODE"))
(command "undo" "mark")
(setvar "CMDECHO" 0)
(setq L (getvar "clayer"))
(setq doit 't)
(while doit

(setq p1 (getpoint "\nPick first point: "))
(setvar "osmode" 512)
(setq p5 (getpoint "\nPick direction of wall to insert along: "))
(setq a2 (angle p1 p5))
(setq Style (getstring "\nInsertion Point at: Wall or Vanity <W>"))
(if (= Style "")
(setq Style "W")
)

(cond ((or (= Style "W")(= Style "w"))
(setq p6 (polar p1 a2 15.5))
)
((or (= Style "V")(= Style "v"))
(setq p6 (polar p1 a2 16.5))
)
)
(setvar "lastpoint" p6)
(setvar "osmode" 128)
(setq P2 (getpoint p6 "\nPick opposite side of wall: "))
(setq a1 (angle p6 p2))
(setq Vent (getstring "\nVent size: 1.5 2 or 3 <1.5>"))
(setq p3 (polar p6 a1 (/ (distance p6 p2) 2)))
(setq p4 (polar p6 (+ a1 pi) 12))

(if (= L "0")
(setq La "1")
(setq La (substr L 1 1)))
(setq Ln (strcat La "SHELL"))

(cond ((= La "F")
(setvar "clayer" "FOUND")
)
((= La "B")
(setvar "clayer" "BSMT")
)
(T (setvar "clayer" Ln)
)
)

(if (= Vent "")
(setq Vent "1.5")
)

(setvar "OSMODE" 0)

(cond
((= B "bswc2")
(command "insert" B p6 1.0 1.0 (+ (R2D a1) 180))
)
)

(setq BV (strcat "DWV Fittings-" Vent))

(setq Ln (strcat La "FLRHH"))
(command "layer" "t" Ln "unlock" Ln "")
(setvar "clayer" Ln)

(command "insert" BV p3 1 1 0)
(command "insert" "DWV Fittings-3" p4 1 1 0)

(setq Ln (strcat La "PLUMBPU"))
(command "layer" "t" Ln "unlock" Ln "")
(setvar "clayer" Ln)

(command "insert" BV p3 1 1 0)
(command "insert" "DWV Fittings-3" p4 1 1 0)

(setq Ln (strcat La "CLGHH"))
(command "layer" "t" Ln "unlock" Ln "")
(setvar "clayer" ln)

(command "insert" BV p3 1 1 0)

(setq p3 nil)
(setq A1 nil)
(setq Ang nil)
(setq Ln nil)
(setq La nil)


(command "-layer" "s" L "")
(command "layerp")
(command "layerp")
(command "layerp")
(command "layerp")
(command "layerp")
(command "layerp")
(command "layerp")
(setvar "cmdecho" 1)
(setvar "OSMODE" os)
)
(princ)
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(DEFUN R2D (R)
(* (/ R PI) 180.0))

(DEFUN D2R (D)
(/ (* D PI) 180.0))


;;;; A little ERROR handling
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(DEFUN *ERROR* (msg)
(setvar "osmode" os)
(setvar "cmdecho" 1)
(setvar "clayer" L)
(setq *ERROR* ERRORSAVE)
)

0 Likes
2,032 Views
10 Replies
Replies (10)
Message 2 of 11

dbroad
Mentor
Mentor

Using ObjectDBX is one way to accomplish your goal. I don't have time to do it for you but the topic has come up from time to time and you may be able to piece together the techniques of using the copyobjects method to get things from one drawing to another.

 

I generally avoid the program approach and simply create palettes from the blocks in my library drawings.  That way, they are easier to manage if I need to change the definition of the block. Right click on the palette and redefine the block works very well.

 

If you're not up to ObjectDBX, I suggest you use library folders instead of library drawings.  That way the insert command is all you need to grab the external definition when needed.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 3 of 11

Anonymous
Not applicable

No, I am not up to date on using ObjectDBX but thanks for the tip to search for.

The code inserts multiple instances of a block on different layers for different level penetrations, and I do not know if I can reference a tool pallete object the way I would like. Will have to break out into separate files so they can be referenced easier.

 

Thanks,

 

Rich

0 Likes
Message 4 of 11

hmsilva
Mentor
Mentor

Hi Rich,

 

using ObjectDBX to import the block definition,

 

;; (copy-blk  "C:\\your\\path\\yourdwgname.dwg" "yourblockname")
;; return T if successful, nil otherwise
(vl-load-com) (defun copy-blk (dwg blk / blkdbx catch odbx) (if (findfile dwg) (progn (if (< (atoi (substr (getvar "ACADVER") 1 2)) 16) (setq odbx (vlax-create-object "ObjectDBX.AxDbDocument")) (setq odbx (vlax-create-object (strcat "ObjectDBX.AxDbDocument." (substr (getvar "ACADVER") 1 2)) ) ) ) (vla-open odbx dwg) (if (not (setq catch (vl-catch-all-error-p (vl-catch-all-apply (function (lambda () (setq blkdbx (vla-item (vla-get-blocks odbx) blk)))) ) ) ) ) (vla-copyobjects odbx (vlax-safearray-fill (vlax-make-safearray vlax-vbObject '(0 . 0)) (list blkdbx) ) (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) ) ) (vlax-release-object odbx) (if (and (null catch) (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) blk ) ) T nil ) ) nil ) )

 

You'll have to test if the block definition exist in the current dwg, using tblsearch or vla-get-blocks, if not, import the block definition and insert the block...

 

Hope that helps

Henrique

 

EESignature

0 Likes
Message 5 of 11

JBerns
Advisor
Advisor

@hmsilva,

 

What changes to your code would be required to make this compatible with AutoCAD 2019?

When I run your code with my supplied drawing and block names,

 

(copy-blk "c:/Temp/Library1.dwg" "Block_A")

an error occurs during the vla-open line.

 

The error displayed:     ; error: Automation Error. Description was not provided.

 

I look forward to any assistance.

 

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 6 of 11

CodeDing
Advisor
Advisor

@JBerns,

 

Didn't realize this post was from years ago. Here would have been my response to the original in its simplest terms...

"I believe you would be able to decode this and use where necessary:

(defun c:TEST ( / ans blockName)
(initget 1 "1.5\" 2\" 3\"")
(setq ans (getkword "\nSelect a size [1.5\"/2\"/3\"]: "))
(setq blockName (strcat "DWV Fittings-" ans))
(if (not (tblsearch "BLOCK" blockName))
	(progn (command "_.INSERT" "Dynamic Blocks.dwg") (command))
);if
(command "_.INSERT" blockName pause "" "" "")
(princ)
);defun

Best,

~DD"

0 Likes
Message 7 of 11

pbejse
Mentor
Mentor

@JBerns wrote:

 

 

The error displayed:     ; error: Automation Error. Description was not provided.

 


 

The source file is currently open.

 

 

 

Message 8 of 11

JBerns
Advisor
Advisor

@CodeDing,

 

Thanks for the reply.

 

However, the Source drawing may contain up to 50 blocks. If only one block is used from it, 49 extra blocks are inserted which now have to be purged. More steps = More time.

 

The goal is to use AutoLISP (preferred) or other programming language to reproduce what the Redefine command does in the Tool Palette context menu.

2019-01-11_7-06-31.png

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 9 of 11

JBerns
Advisor
Advisor

@pbejse,

 

Thanks for the reply and solution.

Wouldn't it be great if error messages actually explained the reason.

 

Good news - bad news.

Good, the code works to insert a single block from a source drawing containing multiple blocks.

Bad, the "duplicate" block definition is ignored. 

 

Still unable to programmatically reproduce the Redefine command available in the Tool Palette context menu.

2019-01-11_7-06-31.png

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 10 of 11

CodeDing
Advisor
Advisor

@JBerns,

 

I am not sure about vl, but here is the AutoLISP version to redefine:

(command "-INSERT" "myblockname=c:/path/myblockname.dwg") (command) 

But you are correct, if the block is in a dwg with multiple other blocks, I am unsure of the best way to accomplish that.

Need to refine my vl skills still.

 

Best,

~DD

0 Likes
Message 11 of 11

JBerns
Advisor
Advisor

@CodeDing,

Yes, your solution works if I had blocks saved as individual drawings.

I leveraged Tool Palettes and the ability to save multiple blocks in a single drawing.

See the example, C:\Program Files\Autodesk\AutoCAD 201x\Sample\en-us\Dynamic Blocks\Mechanical - Imperial.dwg

 

Imagine you have inserted the Hex Head Bolt - Imperial block from the Mechanical tab of the Tool Palette into your drawing.

2019-01-11_8-50-46.png

Later, someone adds more sizes to the original block in the source drawing.

You can use the Tool Palette context menu option, Redefine, to update your inserted blocks to now have the new sizes available.

I want to do this block redefine programmatically. Note, the block is not a standalone drawing.

I will know which blocks come from which source drawings.

You cannot use INSERT to access individual blocks in a Source drawing.

 

 

Regards,

Jerry

 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes