Author Topic: Export Model+Single Layout to Seperate DWG  (Read 3940 times)

0 Members and 1 Guest are viewing this topic.

ifncdylan

  • Guest
Export Model+Single Layout to Seperate DWG
« on: May 15, 2016, 09:22:55 PM »
Hi all,

Did lots of searching for something existing, but I haven't found anything that suits what I need. Exporting layouts to DWG is fine, but I need to export each of my layouts to a new DWG file with that one layout (in a layout still) but preserve all of the model information:

  • 150 layout sheets referencing a large model space, annotations are written on the paperspace over the viewports (don't ask me why!)
  • Client wants every sheet in a seperate DWG file with it's own model space
  • Optionally I'd like to be able to purge linework that isn't referenced in each file

I haven't actually attempted to write anything yet, but I wanted to ask for some guidance. My brute force method would be:

  • Set undo point
  • Remove all layouts except current
  • Write to new file with current layout's name
  • Undo to undo point, move to next paper space
  • Start again

Is this a suitable method, using undo points over and over again? There is a tonne of information, it might take forever or be prone to failure - could anyone give me some advice on performing this operation or possibly point me in the direction of a LISP that could achieve something like this?

ifncdylan

  • Guest
Re: Export Model+Single Layout to Seperate DWG
« Reply #1 on: May 15, 2016, 09:36:53 PM »
As is always the case when I post, I immediately then notice something that helps!  :2funny:

Thanks to T.Willey for the code below:
http://www.theswamp.org/index.php?topic=19721.msg240368#msg240368
Code: [Select]
(defun c:Tabs->Drawings (/ ActDoc MsObjList PsObjList dbxApp oVer FirstPass)

(setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
(vlax-for obj (vla-get-ModelSpace ActDoc)
(setq MsObjList (cons obj MsObjList))
)
(vlax-for lo (vla-get-Layouts ActDoc)
(if (/= (vla-get-Name lo) "Model")
(progn
(setq FirstPass T)
(vlax-for obj (vla-get-Block lo)
(if (not FirstPass)
(setq PsObjList (cons obj PsObjList))
(setq FirstPass nil)
)
)
(setq dbxApp
(if (< (atoi (setq oVer (substr (getvar "acadver") 1 2))) 16)
(vla-GetInterfaceObject (vlax-get-acad-object) "ObjectDBX.AxDbDocument")
(vla-GetInterfaceObject (vlax-get-acad-object) (strcat "ObjectDBX.AxDbDocument." oVer))
)
)
(vlax-invoke ActDoc 'CopyObjects MsObjList (vla-get-ModelSpace dbxApp))
(vlax-invoke ActDoc 'CopyObjects PsObjList (vla-get-PaperSpace dbxApp))
(vla-SaveAs dbxApp (strcat (vla-get-Path ActDoc) "\\" (vla-get-Name lo)))
(vlax-release-object dbxApp)
(setq dbxApp nil)
(setq PsObjList nil)
)
)
)
(princ)
)

There is just one issue - it creates files with two layouts named Layout1 (with the correct layout in it) and then Layout2 (the default layout). Attempting to solve that now, but I'm very unfamiliar with the ObjectDBX stuff in the code I've pasted above, so it might be a bit of trial and error.

ifncdylan

  • Guest
Re: Export Model+Single Layout to Seperate DWG
« Reply #2 on: May 15, 2016, 11:13:51 PM »
Alright, I think I've officially hit my wall of researchability.

I'm trying to modify the above LISP so that it writes only one layout to the new file which is named the same as the layout it exported from the source file. It currently writes two layouts (the default ones) with all the objects in the first layout copied in.

When I manually run the dbxApp code, it creates some kind of blank file from what I can tell. This contains the default Layout1 and Layout2, but I cannot use vla-put-name on these layouts, it just says "Layout does not exist for renaming". The same with trying to delete the second layout:

Code: [Select]
(vla-put-name (vla-get-layout (vla-get-PaperSpace dbxApp)) (vla-get-name lo))
  (vla-delete (vla-item (vla-get-layouts dbxApp) 1))

I've tried running those before and after the CopyObjects is done. I can manually break the script and mess with the objects, they seem to work fine but they just don't allow renaming/deleting. Is this because I'm using vla-command instead of vlax-invoke?

Any help appreciated :)

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Export Model+Single Layout to Seperate DWG
« Reply #3 on: May 16, 2016, 10:18:52 AM »
There are several issues with the proposed code:
  • The code copies to a new drawing that is not based on a template. So you can expect certain drawing variables to have different values.
  • The plot settings of the layout are not copied.
  • The first element inside the PS block is skipped.
But if you want to stick to the code:
Try cloning the layout object using the copyobjects method. This does not work in BricsCAD, but I believe it is possible in AutoCAD.
Before trying to delete a layout make sure it is not the MS layout.

Edit: The first element in the PS block is skipped because it is the main PS viewport.
« Last Edit: May 16, 2016, 10:49:12 AM by roy_043 »

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Export Model+Single Layout to Seperate DWG
« Reply #4 on: May 16, 2016, 10:52:12 AM »
There seems to be no way to edit the name of the layout when opened with objectDBX:
https://www.theswamp.org/index.php?topic=25331.msg305024#msg305024
https://www.theswamp.org/index.php?topic=44543.0

If you want to copy the plot settings of the layout, then maybe you can use the CopyFrom method of the layouts.  I think I have done this before, but not sure how it works with ODBX.

You are correct Roy about skipping the first object in the paper space.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Export Model+Single Layout to Seperate DWG
« Reply #5 on: May 16, 2016, 03:28:13 PM »
After some better testing I find that the situation is slightly different in BricsCAD. The PS blocks in the 'default' odbx document are empty. So the main PS viewport should not be skipped in BricsCAD. Including that viewport means that the order of PsObjList is important, it must match the original order.

Note: I have not introduced BricsCAD-specific functions (notably: vle-collection->list).

Code - Auto/Visual Lisp: [Select]
  1. (defun c:Tabs->Drawings_BricsCAD (/ ActDoc MsObjList PsObjList dbxApp)
  2.   (vlax-for obj (vla-get-modelspace ActDoc)
  3.     (setq MsObjList (cons obj MsObjList))
  4.   )
  5.   (setq MsObjList (reverse MsObjList)) ; Reverse not required.
  6.   (vlax-for lo (vla-get-layouts ActDoc)
  7.     (if (/= (vla-get-name lo) "Model")
  8.       (progn
  9.         (vlax-for obj (vla-get-block lo)
  10.           (setq PsObjList (cons obj PsObjList)) ; Include the main PS viewport.
  11.         )
  12.         (setq dbxApp (vla-getinterfaceobject (vlax-get-acad-object) "ObjectDBX.AxDbDocument"))
  13.         (vlax-invoke ActDoc 'copyobjects MsObjList (vla-get-modelspace dbxApp))
  14.         (vlax-invoke ActDoc 'copyobjects (reverse PsObjList) (vla-get-paperspace dbxApp)) ; Reverse required.
  15.         (vla-copyfrom (vla-get-layout (vla-get-paperspace dbxApp)) lo) ; As advised by T.Willey.
  16.         (vla-saveas dbxApp (strcat (vla-get-path ActDoc) "\\" (vla-get-name lo)))
  17.         (vlax-release-object dbxApp)
  18.         (setq dbxApp nil)
  19.         (setq PsObjList nil)
  20.       )
  21.     )
  22.   )
  23.   (princ)
  24. )

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Export Model+Single Layout to Seperate DWG
« Reply #6 on: May 18, 2016, 03:48:23 AM »
Alternative approach:
Create copies of original dwg.
Erase layouts in copies using 'odbx'.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:ExportLayouts ( / errorCnt odbxObj path trg)
  2.   (if
  3.     (or
  4.       (= 0 (getvar 'dwgtitled))
  5.       (/= 0 (getvar 'dbmod))
  6.     )
  7.     (princ "\nError: drawing not saved ")
  8.     (progn
  9.       (setq errorCnt 0)
  10.       (setq odbxObj
  11.           (vlax-get-acad-object)
  12.           (if (> 16 (atoi (substr (getvar 'acadver) 1 2)))
  13.             "ObjectDBX.AxDbDocument"
  14.             (strcat "ObjectDBX.AxDbDocument." (substr (getvar 'acadver) 1 2))
  15.           )
  16.         )
  17.       )
  18.       (setq path (strcat (getvar 'dwgprefix) (vl-filename-base (getvar 'dwgname))))
  19.       (foreach lytNme (layoutlist)
  20.         (if
  21.           (and
  22.             (or
  23.               (not (findfile (setq trg (strcat path "_" lytNme ".dwg"))))
  24.               (vl-file-delete trg)
  25.             )
  26.             (vl-file-copy (strcat path ".dwg") trg) ; Copy original drawing.
  27.           )
  28.           (progn
  29.             (vla-open odbxObj trg)
  30.             (vlax-for lytObj (vla-get-layouts odbxObj)
  31.               (if
  32.                 (and
  33.                   (= :vlax-false (vla-get-modeltype lytObj))
  34.                   (/= lytNme (vla-get-name lytObj))
  35.                 )
  36.                 (vla-delete lytObj)
  37.               )
  38.             )
  39.             (vla-saveas odbxobj trg) ; (vla-save odbxobj) doesn't work in AC. Thanks ronjonp.
  40.           )
  41.           (setq errorCnt (1+ errorCnt))
  42.         )
  43.       )
  44.       (vlax-release-object odbxObj)
  45.       (if (/= 0 errorCnt)
  46.         (princ (strcat "\nError: " (itoa errorCnt) " layout(s) not exported "))
  47.       )
  48.     )
  49.   )
  50.   (princ)
  51. )
« Last Edit: May 18, 2016, 11:39:24 AM by roy_043 »

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Export Model+Single Layout to Seperate DWG
« Reply #7 on: May 18, 2016, 09:21:46 AM »
Roy,
Not sure about Briscad but in AutoCAD you have to use vla-saveas on a dbx doc.
Code - Auto/Visual Lisp: [Select]
  1. (vla-saveas odbxobj trg)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Export Model+Single Layout to Seperate DWG
« Reply #8 on: May 18, 2016, 11:44:13 AM »
Roy,
Not sure about Briscad but in AutoCAD you have to use vla-saveas on a dbx doc.
Code - Auto/Visual Lisp: [Select]
  1. (vla-saveas odbxobj trg)
Thanks for mentioning this. I have corrected the code. In BricsCAD vla-save can be used as well.

Code: [Select]
(vlax-dump-object (vla-getinterfaceobject (vlax-get-acad-object) "objectdbx.axdbdocument") T)=>
Code: [Select]
;
; Property values (IAxDbDocument) :
;
;   Application (RO) = #<VLA-OBJECT IAcadApplication 0CA2A918>
;   Database (RO) = #<VLA-OBJECT IAcadDatabase 0CBF6D94>
;   Name =
;
; Methods supported (IAxDbDocument) :
;
;   DxfIn (2)
;   DxfOut (3)
;   Open (2)
;   Save ()
;   SaveAs (2)
;
; Properties and Methods of associated Database object :
;
; IAcadDatabase 0cbf6d94 : Provides an interface to the database resident objects in an TeighaX document
;
; Property values :
;
;   Blocks (RO) = #<VLA-OBJECT IAcadBlocks 0CAD7B60>
;   Dictionaries (RO) = #<VLA-OBJECT IAcadDictionaries 0CBD0160>
;   DimStyles (RO) = #<VLA-OBJECT IAcadDimStyles 0CBD01A0>
;   ElevationModelSpace = 0.0
;   ElevationPaperSpace = 0.0
;   FileDependencies (RO) = #<VLA-OBJECT IAcadFileDependencies 0CBF74B8>
;   Groups (RO) = #<VLA-OBJECT IAcadGroups 0CAD7AE0>
;   Layers (RO) = #<VLA-OBJECT IAcadLayers 0CBD01E0>
;   Layouts (RO) = #<VLA-OBJECT IAcadLayouts 0CAD7B20>
;   Limits = (0.0 0.0 12.0 9.0)
;   Linetypes (RO) = #<VLA-OBJECT IAcadLineTypes 0CBF8EB0>
;   Materials (RO) = #<VLA-OBJECT IAcadMaterials 0CBF8F28>
;   ModelSpace (RO) = #<VLA-OBJECT IAcadModelSpace 0CBF8FA0>
;   PaperSpace (RO) = #<VLA-OBJECT IAcadPaperSpace 0CBF9018>
;   PlotConfigurations (RO) = #<VLA-OBJECT IAcadPlotConfigurations 0CBF9090>
;   Preferences (RO) = #<VLA-OBJECT IAcadDatabasePreferences 0CBF8F8C>
;   RegisteredApplications (RO) = #<VLA-OBJECT IAcadRegisteredApplications 0CBF9108>
;   SectionManager (RO) = NIL
;   SummaryInfo (RO) = #<VLA-OBJECT IAcadSummaryInfo 0CBF907C>
;   TextStyles (RO) = #<VLA-OBJECT IAcadTextStyles 0CBF9298>
;   UserCoordinateSystems (RO) = #<VLA-OBJECT IAcadUCSs 0CBF92D8>
;   Viewports (RO) = #<VLA-OBJECT IAcadViewports 0CBF93A8>
;   Views (RO) = #<VLA-OBJECT IAcadViews 0CBF9420>
;
; Methods supported :
;
;   CopyObjects (3)
;   HandleToObject (1)
;   ObjectIdToObject (1)
T

trogg

  • Bull Frog
  • Posts: 255
Re: Export Model+Single Layout to Seperate DWG
« Reply #9 on: May 18, 2016, 12:00:08 PM »
I have had great results from Jimmy's lisp code "LayoutsToDwgs.lsp Creates separate drawings of all layouts"

Found here:http://jtbworld.com/autolisp-visual-lisp

~Greg

sasi

  • Mosquito
  • Posts: 3
Re: Export Model+Single Layout to Seperate DWG
« Reply #10 on: December 22, 2017, 01:14:58 AM »
PFA lisp it will help you