TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: AIR on March 23, 2022, 10:47:20 AM

Title: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: AIR on March 23, 2022, 10:47:20 AM
Hello Ladies and Gents,

My first post here.
I'm looking for your help programming gurus!
My initial topic is described and discussed in here (https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/export-top-level-and-nested-blocks/td-p/10479819/page/2)
But discussion was left without resolution. Maybe Swamp community can resolve it. Thanks in advance.

Input data:
- I have mechanical assembly originally developed in Inventor. No access to original available.
- Original was exported to step/iges or other format. Information of format not available.
- Export format was imported into AutoCAD file. 1x DWG file
- Blocks, blocks names and nested blocks structure resembles original Inventor structure.
- Amount of blocks - more than 20k. Technically all those blocks are Inventor parts.
- Many blocks have duplicates. Ex. bolts, nuts, etc.
- DWG is set to UCS.

My task/issue:
- From DWG file I need to extract all blocks into separate DWG files and retain UCS of parent DWG file, for all extracted blocks.
- Every extracted DWG file should be named in the name of the block it belonged
- If blocks are duplicated, they can be retained in 1 collective DWG file. Ex. original DWG, block name: M20 bolt -->> extracted DWG named M20 bolt, containing all M20 bolts from original DWG
   --> Otherwise, they can be suffixed/prefixed with any chosen value.
   --> Best to have both options if possible, collective dwg and possibility to extract duplicates as suffixed/prefixed.

Info:
- Test DWG attached
- Lisp file (not mine) attached. Lisp is already doing great work, however extracted DWG files are reset to WCS


Can you please suggest portion of code to be added to lisp file, such that extracted DWG files would not reset to WCS, but keep UCS of the parent file.

Thanks in advance!
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: ronjonp on March 23, 2022, 11:23:47 AM
No one is going to run that VLX file .. I take it that you did not get a final answer here?
https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/export-top-level-and-nested-blocks/td-p/10479819
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: AIR on March 24, 2022, 04:24:34 AM
No one is going to run that VLX file

Why not? What's wrong with it?

I take it that you did not get a final answer here?
https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/export-top-level-and-nested-blocks/td-p/10479819

It is what I've stated at the beginning of the topic.
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: ronjonp on March 24, 2022, 09:11:01 AM
No one is going to run that VLX file

Why not? What's wrong with it?

I take it that you did not get a final answer here?
https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/export-top-level-and-nested-blocks/td-p/10479819

It is what I've stated at the beginning of the topic.
You can't see the source in compiled code and could be malicious ... I won't run it.

Sorry .. I didn't read your post closely. Maybe you'll get someone to help out.
I was still a bit confused after following the thread at the other forum.

As mentioned before: "Every extracted DWG file should be named in the name of the block it belonged" This will create a circular reference.
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: AIR on March 24, 2022, 10:05:16 AM
As mentioned before: "Every extracted DWG file should be named in the name of the block it belonged" This will create a circular reference.

Well, lisp is 90% of what I need, but since you don't want to run it ;).....

What lisp does, is basically WBLOCK'ing all blocks inside dwg into separate files, but resetting UCS to WCS
I'm not sure if "circular ref" is an issue. If you take all blocks with same name and write them into one file, how circular ref is created?
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: AIR on March 24, 2022, 10:12:13 AM
My logic of potential script described as following

1. create variable (C_USER) with coordinates of original drawing.
2. create list of all unique blocks (names)
3. (here a loop) ref copy (at original 0,0,0) block(-s) with unique names at all nested levels (if any)
4. create empty file
5. change coordinates system to C_USER of empty file
6. paste copied block(-s)
7. save with the block name from the list
8. iterate until finished....
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: ronjonp on March 24, 2022, 10:47:09 AM
As mentioned before: "Every extracted DWG file should be named in the name of the block it belonged" This will create a circular reference.

Well, lisp is 90% of what I need, but since you don't want to run it ;).....
?
It doesn't do me any good to run that code ... it can't be changed because it's compiled. Post the source code and maybe someone can help.
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: AIR on March 24, 2022, 01:09:03 PM
It doesn't do me any good to run that code ... it can't be changed because it's compiled. Post the source code and maybe someone can help.

More than that I don't have. I'm not author of the code.
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: AIR on March 24, 2022, 01:53:51 PM
Maybe following code can be of any value. Seems it does similar job as VLX file.

Code: [Select]
;;; --------------------------------------------------------;;

          ;;; MWBLOCK.LSP (Multiple WBlock)

          ;;; English translated 

          ;;;

          ;;; --------------------------------------------------------;;

          ;;;

            (defun DXF (C E / )(cdr (assoc C E)))

          ;;;

          ;;;

          ;;;

            (defun BLOCK_LIST ( / DBL LBL)

              (while

                (setq DBL (tblnext "BLOCK" (not DBL)))

                (if

                  (/= "*" (substr (DXF 2 DBL) 1 1))

                  (setq LBL

                    (append LBL (list (DXF 2 DBL)))

                  )

                )

              )

              (if LBL

                (setq LBL (acad_strlsort LBL))

                (alert "You have not any blocks in a drawing ")

              )

              (if LBL LBL NIL)

            )

          ;;;

          ;;;

          ;;;

            (defun C:MBL ( / FIRSTNAME LBL PTH X CMD INC)

              (setq INC 0

                    LBL (BLOCK_LIST)

              )

              (if

                (= NIL LBL)

                (princ "\nDone. ")

                (progn

                  (setq FIRSTNAME

                    (getfiled "Select Export Directory" (car LBL) "dwg" 15)

                  )

                  (if FIRSTNAME

                    (progn

                    (setq PTH (car (fnsplitl FIRSTNAME)))

                    (setq CMD (getvar "CMDECHO"))

                    (setvar "CMDECHO" 0)

                    (foreach X LBL

                      (if

                        (findfile (strcat PTH X ".dwg"))

                        (command "_.WBLOCK" (strcat PTH X) "_N")

                        (progn

                          (command "_.WBLOCK" (strcat PTH X) "=")

                          (princ ".")

                          (setq INC (1+ INC))

                        )

                      )

                    )

                    (setvar "CMDECHO" CMD)

                  )

                  (princ "\nDone. ")

                )

              )

            )

            (if PTH

              (alert

                (strcat "\nWriten "

                        (itoa INC)

                        " blocks in cirectory "

                        (strcase PTH)

                )

              )

            )

            (princ)

            )

          ;;;

          ;;;

          ;;;

            (princ)
(princ "\n Export All blocks in Drawing - MBL.")
            (princ)

(c:mbl)
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: ronjonp on March 24, 2022, 03:26:28 PM
Maybe following code can be of any value. Seems it does similar job as VLX file.

Code: [Select]
;;; --------------------------------------------------------;;

          ;;; MWBLOCK.LSP (Multiple WBlock)

          ;;; English translated 

          ;;;

          ;;; --------------------------------------------------------;;

          ;;;

            (defun DXF (C E / )(cdr (assoc C E)))

          ;;;

          ;;;

          ;;;

            (defun BLOCK_LIST ( / DBL LBL)

              (while

                (setq DBL (tblnext "BLOCK" (not DBL)))

                (if

                  (/= "*" (substr (DXF 2 DBL) 1 1))

                  (setq LBL

                    (append LBL (list (DXF 2 DBL)))

                  )

                )

              )

              (if LBL

                (setq LBL (acad_strlsort LBL))

                (alert "You have not any blocks in a drawing ")

              )

              (if LBL LBL NIL)

            )

          ;;;

          ;;;

          ;;;

            (defun C:MBL ( / FIRSTNAME LBL PTH X CMD INC)

              (setq INC 0

                    LBL (BLOCK_LIST)

              )

              (if

                (= NIL LBL)

                (princ "\nDone. ")

                (progn

                  (setq FIRSTNAME

                    (getfiled "Select Export Directory" (car LBL) "dwg" 15)

                  )

                  (if FIRSTNAME

                    (progn

                    (setq PTH (car (fnsplitl FIRSTNAME)))

                    (setq CMD (getvar "CMDECHO"))

                    (setvar "CMDECHO" 0)

                    (foreach X LBL

                      (if

                        (findfile (strcat PTH X ".dwg"))

                        (command "_.WBLOCK" (strcat PTH X) "_N")

                        (progn

                          (command "_.WBLOCK" (strcat PTH X) "=")

                          (princ ".")

                          (setq INC (1+ INC))

                        )

                      )

                    )

                    (setvar "CMDECHO" CMD)

                  )

                  (princ "\nDone. ")

                )

              )

            )

            (if PTH

              (alert

                (strcat "\nWriten "

                        (itoa INC)

                        " blocks in cirectory "

                        (strcase PTH)

                )

              )

            )

            (princ)

            )

          ;;;

          ;;;

          ;;;

            (princ)
(princ "\n Export All blocks in Drawing - MBL.")
            (princ)

(c:mbl)
Try this .. although you're probably going to have the UCS issue too. If you could make one file of one block how you want the results to be then maybe I can help.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:foo (/ p)
  2.   (setvar 'cmdecho 0)
  3.   (setq p (strcat (getvar 'dwgprefix) "_ExportedBlocks\\"))
  4.   (vl-mkdir p)
  5.     (if (= 0 (vlax-get b 'isxref) (vlax-get b 'islayout))
  6.       (command "._-wblock" (strcat p (vla-get-name b) ".dwg") (vla-get-name b))
  7.     )
  8.   )
  9.   (setvar 'cmdecho 1)
  10.   (alert p)
  11.   (princ)
  12. )
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: AIR on March 25, 2022, 04:15:21 AM
Try this .. although you're probably going to have the UCS issue too. If you could make one file of one block how you want the results to be then maybe I can help.
Thanks so far. It seems your code has same operation as my posted code, with exception there is no dialog anymore and files are saved into folder.
Indeed, UCS is again reset to WCS

Here is my initial goal:

- From DWG file I need to extract all blocks into separate DWG files and retain UCS of parent DWG file, for all extracted blocks.
- Every extracted DWG file should be named in the name of the block it belonged
- If blocks are duplicated (same name), they can be retained in 1 collective DWG file. Ex. original DWG, block name: M20 bolt -->> extracted DWG named M20 bolt, containing all M20 bolts from original DWG
   --> Otherwise, they can be suffixed/prefixed with any chosen value.
   --> Best to have both options if possible, collective dwg and possibility to extract duplicates as suffixed/prefixed.
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: ronjonp on March 25, 2022, 10:46:14 AM
I think you are confusing block definitions with block references.
The wblock command pulls the definition of the block into a new drawing and the reference is the inserted block or blocks of that reference. I still need an example drawing of what you're expecting for an end result.
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: AIR on March 25, 2022, 11:48:32 AM
Like this for example. File contains all blocks nr. 112. But you see UCS in extracted dwg is now reset to WCS. I need UCS of parent drawing in extracted files.
It would be nice to have an option to extract all block references into 1 dwg or to extract them into unique files with prefix/suffix.
  OR--> TEST_BLOCK_112.dwg - with all blocks  nr.112
  OR--> TEST_BLOCK_112_1....X.dwg - unique file per block refence with suffix
  OR--> 1...X_TEST_BLOCK_112.dwg - unique file per block refence with prefix
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: AIR on March 30, 2022, 03:00:15 AM
I think you are confusing block definitions with block references.
The wblock command pulls the definition of the block into a new drawing and the reference is the inserted block or blocks of that reference. I still need an example drawing of what you're expecting for an end result.

Ronjonp, any thought on above? Thanks so far!
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: ronjonp on March 30, 2022, 01:33:16 PM
I think you are confusing block definitions with block references.
The wblock command pulls the definition of the block into a new drawing and the reference is the inserted block or blocks of that reference. I still need an example drawing of what you're expecting for an end result.

Ronjonp, any thought on above? Thanks so far!
I'm failing to see how you're going to 'reuse' these blocks ( albeit I'm not a 3D user much )?
WBLOCK is pretty standard for extracting block DEFINITIONS .. you want block REFERENCES within a separate drawing so essentially nested blocks.

This is where those 4 bolts insert into your example assembly:
Title: Re: EXTRACT TOP LEVEL AND NESTED BLOCKS
Post by: AIR on March 31, 2022, 03:57:26 AM
I'm failing to see how you're going to 'reuse' these blocks ( albeit I'm not a 3D user much )?

I need all those blocks in the separate files with parent origin, so I can reassemble them in other configurations of the machine.

WBLOCK is pretty standard for extracting block DEFINITIONS .. you want block REFERENCES within a separate drawing so essentially nested blocks.

Yes, indeed, references of unique blocks in one file. Unique blocks are "nested" within one file. So, one file - one block definition, with (potentially) several references of the same block.

This is where those 4 bolts insert into your example assembly:

Yes indeed, I know location of many components.