Jump to content

Block Extents to View


dmac

Recommended Posts

I'm trying to modify some code to achieve the following:

 

Ask the user to pick multiple blocks, step through each block, get the extents of the block, and save a view with the view name equal to the name of the block.  Doing some research I've managed to get the extents of the block but can't figure out how to store the block name as a variable and use it as the view name.  The code I found will draw a rectangle around the block using vla-getboundingbox but I can't figure out how to assign the name of the block to a variable for use in the -view command.  I have a few hundred blocks in the same drawing.  Existing code is:

 

(defun c:BlockExtentsToRectangle (/ BlockObject mn mx)
 (vl-load-com)
 (if (setq BlockObject (car (entsel "\n  >>  Select Object  >> ")))
   (progn
     (vla-getboundingbox (vlax-ename->vla-object BlockObject) 'mn 'mx)
     (vl-cmdf "._rectang" (vlax-safearray->list mn) (vlax-safearray->list mx))))
 (princ)
)

 

Link to comment
Share on other sites

Have a look a either

(setq bname (cdr (assoc 2 (entget blockobject))))

(setq bname (vla-get-name (vlax-ename->vla-object BlockObject)))

Look at this to see the dxf number I often just type direct on command line

(entget (car (entsel "\nPick objects")))

 

Edited by BIGAL
Link to comment
Share on other sites

Thanks BIGAL, this is what I ended up with in case someone needs the same thing.  My guess is this can be refined somewhat, but it works!

 

(defun c:BlockExtentsToView (/ BlockObject BlockName mn mx);Define function
 (vl-load-com);Load the extended AutoLISP functions
 (setq BlockObject (car (entsel "\n  >>  Select Object  >> ")));Select block
   (setq BlockName (cdr (assoc 2 (entget blockobject))));Assign name of block to variable
(setq BlockName (vla-get-name (vlax-ename->vla-object BlockObject)));Convert ename to vla object 
   (progn;Begin evaluation of multiple expressions
     (vla-getboundingbox (vlax-ename->vla-object BlockObject) 'mn 'mx);Get block extents
          (vl-cmdf "._view" "w" BlockName (vlax-safearray->list mn) (vlax-safearray->list mx));Create view
   );End progn
  (princ);Suppress nil
 );End function

I think I can figure out how to select multiple blocks and put this in a loop.  I'll post that once it's done.  May take a while, I do this kind of thing in my "free time".

Link to comment
Share on other sites

Hi @dmac please give it a test , it return the view names list ,.

(DEFUN C:ALL--BLOCKEXTENTSTOVIEW  (/ BLOCKOBJECT BLOCKNAME MN MX) ;Define function
  (VL-LOAD-COM) ;Load the extended AutoLISP functions
  (SETQ ACADOBJ (VLAX-GET-ACAD-OBJECT))
  (SETQ DOC (VLA-GET-ACTIVEDOCUMENT ACADOBJ))
  (SETQ VIEWCOLL (VLA-GET-VIEWS DOC))
   
  (PRINC "\n Select the blk-references")

  (SETQ BLK-REF-SS (SSGET '((0 . "insert"))))

  (REPEAT (SETQ I (SSLENGTH BLK-REF-SS))
    (SETQ BLOCKOBJECT (VLAX-ENAME->VLA-OBJECT (SSNAME BLK-REF-SS (SETQ I (1- I)))))
    (SETQ BLOCKNAME (VLA-GET-NAME BLOCKOBJECT)) ;get name 
 ;
    (PROGN ;Begin evaluation of multiple expressions
      (VLA-GETBOUNDINGBOX BLOCKOBJECT 'MN 'MX) ;Get block extents
      (VL-CMDF "._view" "w" BLOCKNAME (VLAX-SAFEARRAY->LIST MN) (VLAX-SAFEARRAY->LIST MX)) ;Create view
      ) ;End progn
    ) ; end repeat



  (SETQ VIEW-NAMES-LST ())
  (VLAX-FOR VIEW  VIEWCOLL
    (SETQ VIEW-NAMES-LST (CONS (VLA-GET-NAME VIEW) VIEW-NAMES-LST))
    )

  (PRINC) ;Suppress nil

  ) ;End function
;|«Visual LISP© Format Options»
(180 2 1 0 nil "end of " 100 20 2 2 nil nil T nil T)
;*** DO NOT add text below the comment! ***|;

 

Link to comment
Share on other sites

Thanks devitg, worked like a charm.  However, what is the purpose of the following:

 

  (SETQ ACADOBJ (VLAX-GET-ACAD-OBJECT))
  (SETQ DOC (VLA-GET-ACTIVEDOCUMENT ACADOBJ))
  (SETQ VIEWCOLL (VLA-GET-VIEWS DOC))

and

(SETQ VIEW-NAMES-LST ())
  (VLAX-FOR VIEW  VIEWCOLL
    (SETQ VIEW-NAMES-LST (CONS (VLA-GET-NAME VIEW) VIEW-NAMES-LST))
    )

I was curious so commented those out and the function still worked as intended.

Link to comment
Share on other sites

Hi,

Here is my way in coding it and it would never duplicate views and would get the correct name from dynamic blocks if selected .

You can disable and enable the system variable CMDECHO to avoid printing the process of the command to command line.

(defun c:Test (/ int sel ent vla bkn lft rgt)
  ;; Tharwat - Date: 14.Apr.2021	;;
  ;; Create views from selected blocks.	;;
  (and (princ "\nSelect Blocks to create views based on their boundary limits : ")
       (setq int -1 sel (ssget '((0 . "INSERT"))))
       (while (setq int (1+ int) ent (ssname sel int))
         (setq vla (vlax-ename->vla-object ent)
               bkn (vla-get-EffectiveName vla)
               )
         (or (tblsearch "VIEW" bkn)
             (or (vla-getboundingbox vla 'lft 'rgt)
                 (and lft rgt 
                      (mapcar 'set '(lft rgt) (mapcar 'vlax-safearray->list (list lft rgt)))
                      (vl-cmdf "_.VIEW" "W" bkn "_none" lft "_none" rgt)
                      )
                 )
             )
         )
       )
  (princ)                           
) (vl-load-com)

 

Link to comment
Share on other sites

7 hours ago, dmac said:

Thanks devitg, worked like a charm.  However, what is the purpose of the following:

 


  (SETQ ACADOBJ (VLAX-GET-ACAD-OBJECT))
  (SETQ DOC (VLA-GET-ACTIVEDOCUMENT ACADOBJ))
  (SETQ VIEWCOLL (VLA-GET-VIEWS DOC))

and


(SETQ VIEW-NAMES-LST ())
  (VLAX-FOR VIEW  VIEWCOLL
    (SETQ VIEW-NAMES-LST (CONS (VLA-GET-NAME VIEW) VIEW-NAMES-LST))
    )

I was curious so commented those out and the function still worked as intended.

 

 

The first line at first part  get the WHOLE acad OBJECT  or acad session  we are on. it hold this  properties and  method  as follow 

; IAcadApplication: An instance of the AutoCAD application
; Property values:
;   ActiveDocument = #<VLA-OBJECT IAcadDocument 000002942397f138>
;   Application (RO) = #<VLA-OBJECT IAcadApplication 00007ff67d44b0b8>
;   Caption (RO) = "Autodesk AutoCAD 2019 - [Drawing1.dwg]"
;   Documents (RO) = #<VLA-OBJECT IAcadDocuments 000002942f123530>
;   FullName (RO) = "C:\\Program Files\\Autodesk\\AutoCAD 2019\\acad.exe"
;   Height = 784
;   HWND (RO) = 66974
;   LocaleId (RO) = 1033
;   MenuBar (RO) = #<VLA-OBJECT IAcadMenuBar 000002943249c2f8>
;   MenuGroups (RO) = #<VLA-OBJECT IAcadMenuGroups 000002947eedc240>
;   Name (RO) = "AutoCAD"
;   Path (RO) = "C:\\Program Files\\Autodesk\\AutoCAD 2019"
;   Preferences (RO) = #<VLA-OBJECT IAcadPreferences 000002941fe44988>
;   StatusId (RO) = ...Indexed contents not shown...
;   VBE (RO) = AutoCAD: Problem in loading VBA
;   Version (RO) = "23.0s (LMS Tech)"
;   Visible = -1
;   Width = 1368
;   WindowLeft = -8
;   WindowState = 3
;   WindowTop = -8
; Methods supported:
;   Eval (1)
;   GetAcadState ()
;   GetInterfaceObject (1)
;   ListArx ()
;   LoadArx (1)
;   LoadDVB (1)
;   Quit ()
;   RunMacro (1)
;   UnloadArx (1)
;   UnloadDVB (1)
;   Update ()
;   ZoomAll ()
;   ZoomCenter (2)
;   ZoomExtents ()
;   ZoomPickWindow ()
;   ZoomPrevious ()
;   ZoomScaled (2)
;   ZoomWindow (2)

As you can see the active document is the firs property and you can get it by the second line  

 

 (SETQ DOC (VLA-GET-ACTIVEDOCUMENT ACADOBJ))

and by  this line 

 

(VLAX-DUMP-OBJECT doc  ) T)

you get all props and method for the active DWG  as follow 

; IAcadDocument: An AutoCAD drawing
; Property values:
;   Active (RO) = -1
;   ActiveDimStyle = #<VLA-OBJECT IAcadDimStyle 0000029431ea9c68>
;   ActiveLayer = #<VLA-OBJECT IAcadLayer 0000029431ea9e08>
;   ActiveLayout = #<VLA-OBJECT IAcadLayout 0000029431eaa218>
;   ActiveLinetype = #<VLA-OBJECT IAcadLineType 0000029431eaafe8>
;   ActiveMaterial = #<VLA-OBJECT IAcadMaterial 0000029431eab0b8>
;   ActivePViewport = AutoCAD: No active viewport in paperspace
;   ActiveSelectionSet (RO) = #<VLA-OBJECT IAcadSelectionSet 00000294322d8888>
;   ActiveSpace = 1
;   ActiveTextStyle = #<VLA-OBJECT IAcadTextStyle 0000029431ea6ee8>
;   ActiveUCS = AutoCAD: Null object ID
;   ActiveViewport = #<VLA-OBJECT IAcadViewport 0000029431ea50d8>
;   Application (RO) = #<VLA-OBJECT IAcadApplication 00007ff67d44b0b8>
;   Blocks (RO) = #<VLA-OBJECT IAcadBlocks 0000029431ea5418>
;   Database (RO) = #<VLA-OBJECT IAcadDatabase 000002942383b638>
;   Dictionaries (RO) = #<VLA-OBJECT IAcadDictionaries 0000029431ea6388>
;   DimStyles (RO) = #<VLA-OBJECT IAcadDimStyles 0000029431ea43d8>
;   ElevationModelSpace = 0.0
;   ElevationPaperSpace = 0.0
;   FullName (RO) = ""
;   Groups (RO) = #<VLA-OBJECT IAcadGroups 0000029431ea4578>
;   Height = 620
;   HWND (RO) = 67184
;   Layers (RO) = #<VLA-OBJECT IAcadLayers 0000029431ea4f38>
;   Layouts (RO) = #<VLA-OBJECT IAcadLayouts 000002942ecfdaa8>
;   Limits = (0.0 0.0 420.0 297.0)
;   Linetypes (RO) = #<VLA-OBJECT IAcadLineTypes 000002942ecfc7f8>
;   Materials (RO) = #<VLA-OBJECT IAcadMaterials 000002942ecfcf48>
;   ModelSpace (RO) = #<VLA-OBJECT IAcadModelSpace 00000294321d7318>
;   MSpace = AutoCAD: Invalid mode
;   Name (RO) = "Drawing1.dwg"
;   ObjectSnapMode = 0
;   PaperSpace (RO) = #<VLA-OBJECT IAcadPaperSpace 0000029431e5c6c8>
;   Path (RO) = "C:\\Users\\gabriel\\Documents"
;   PickfirstSelectionSet (RO) = #<VLA-OBJECT IAcadSelectionSet 0000029cc62ba9a8>
;   Plot (RO) = #<VLA-OBJECT IAcadPlot 000002943228b578>
;   PlotConfigurations (RO) = #<VLA-OBJECT IAcadPlotConfigurations 000002942ecfd1b8>
;   Preferences (RO) = #<VLA-OBJECT IAcadDatabasePreferences 0000029431a9a318>
;   ReadOnly (RO) = 0
;   RegisteredApplications (RO) = #<VLA-OBJECT IAcadRegisteredApplications 000002942ecfc318>
;   Saved (RO) = 0
;   SectionManager (RO) = Ocurrió una excepción
;   SelectionSets (RO) = #<VLA-OBJECT IAcadSelectionSets 0000029cc65d24e8>
;   SummaryInfo (RO) = #<VLA-OBJECT IAcadSummaryInfo 0000029431a9ba18>
;   TextStyles (RO) = #<VLA-OBJECT IAcadTextStyles 000002942ecfca68>
;   UserCoordinateSystems (RO) = #<VLA-OBJECT IAcadUCSs 000002942ecfcda8>
;   Utility (RO) = #<VLA-OBJECT IAcadUtility 0000029433b70ab8>
;   Viewports (RO) = #<VLA-OBJECT IAcadViewports 000002942ecfc3e8>
;   Views (RO) = #<VLA-OBJECT IAcadViews 000002942ecfd428>
;   Width = 1251
;   WindowState = 3
;   WindowTitle (RO) = "Drawing1.dwg"
; Methods supported:
;   Activate ()
;   AuditInfo (1)
;   Close (2)
;   CopyObjects (3)
;   EndUndoMark ()
;   Export (3)
;   GetVariable (1)
;   HandleToObject (1)
;   Import (3)
;   LoadShapeFile (1)
;   New (1)
;   ObjectIdToObject (1)
;   Open (2)
;   PostCommand (1)
;   PurgeAll ()
;   Regen (1)
;   Save ()
;   SaveAs (3)
;   SendCommand (1)
;   SetVariable (2)
;   StartUndoMark ()
;   Wblock (2)

As properties hold among others , collections  like 

 

;   Layers (RO) = #<VLA-OBJECT IAcadLayers 0000029431ea4f38>
;   Layouts (RO) = #<VLA-OBJECT IAcadLayouts 000002942ecfdaa8>
;   Limits = (0.0 0.0 420.0 297.0)
;   Linetypes (RO) = #<VLA-OBJECT IAcadLineTypes 000002942ecfc7f8>
;   Materials (RO) = #<VLA-OBJECT IAcadMaterials 000002942ecfcf48>
;   SelectionSets (RO) = #<VLA-OBJECT IAcadSelectionSets 0000029cc65d24e8>
;   SummaryInfo (RO) = #<VLA-OBJECT IAcadSummaryInfo 0000029431a9ba18>
;   TextStyles (RO) = #<VLA-OBJECT IAcadTextStyles 000002942ecfca68>
;   UserCoordinateSystems (RO) = #<VLA-OBJECT IAcadUCSs 000002942ecfcda8>
;   Utility (RO) = #<VLA-OBJECT IAcadUtility 0000029433b70ab8>
;   Viewports (RO) = #<VLA-OBJECT IAcadViewports 000002942ecfc3e8>
;   Views (RO) = #<VLA-OBJECT IAcadViews 000002942ecfd428>

then the 3rd line hold de VIEWS collection , hold the views , 

 

(SETQ VIEWCOLL (VLA-GET-VIEWS DOC))

And by that second part give you a list of the named views at this DOC from the ACADOBJ, 

as it do a VLAX-FOR  view viewcoll  ( some like the foreach at plain LISP)

getting it's name by 

 

SETQ VIEW-NAMES-LST (CONS (VLA-GET-NAME VIEW) VIEW-NAMES-LST))

 Hope I made a good explanation 

 

 

Link to comment
Share on other sites

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...