Automating the Layout Generation and Page Setup

Automating the Layout Generation and Page Setup

parikhnidi
Advocate Advocate
1,853 Views
2 Replies
Message 1 of 3

Automating the Layout Generation and Page Setup

parikhnidi
Advocate
Advocate

Hi,

 

I am trying to automate my work as follows –

 

I have an Access query that has finalized my calculations and I am able to draw objects with different configurations as follows –

 

The rs below is a simple snapshot of query at runtime I indicated above.

 

ObjectLocation(0) = 0
ObjectLocation(1) = 0
ObjectLocation(2) = 0
i = 1
Do While Not rs.EOF
     DrawObject at ObjectLocation in modelspace
     ObjectLocation(0) = ObjectLocation(0) + 5000
     'What I would like to do here is –
     ‘1. Create a new layout tab
     ‘2. Assign that layout a name as “Column “& i
     ‘3. Set paper size to A0
     ‘4. Create new Paperspace Viewport
     ‘4.1 Set newly created viewport height to rs.Fields(“Height”).Value
     ‘4.2 Set newly created viewport width to rs.Fields(“Width”).Value
     ‘4.3 Goto Modelspece from newly created viewport
     ‘4.4 Zoom viewport center to ObjectLocation
     '4.5 Set Height to rs.Fields(“TotalHeight”).Value
     Come back to paperspace.
     i = i + 1
     rs.movenext
Loop

 

The DrawObject process by itself is very complex and it is well accomplished. However, preparing drawing on Layout is where I am stuck up. I am not strong with the Layout / Plot and their configuration within VBA.

 

Can anyone provide me with the skeleton code, that I can piggyback on.

 

If desired, I will upload my sample database for the ready reference.

 

Nimish

@grobnikI've not yet dipped my toes into the link you provided below

https://forums.autodesk.com/t5/vba/vba-plotting-page-setups/td-p/1198553

 

0 Likes
1,854 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

For your listed step 1 - 4.5 inside the Do...Loop (after your code draws stuff in the model space, the code for these steps would be something like (pseudo code):

 

'' 1 & 2: create a new layout and give it a proper name

Dim lay As AcadLayout

Set lay=ThisDrawing.Layouts("[layoutName]")

 

'' 3: set layout paper

lay.ConfigName="Printer/PC3 name]"

'' set other Layout properties accordingly...

 

''  4: Open viewport on this layout

ThisDrawing.ActiveLayout=lay

Dim vport As AcadPViewport

Set vport=ThisDrawing.PaperSpace.AddPViewport(center, width, height)

'' set up the viewport to show content in ModelSpace properly.

 

With above said, here are a couple of suggestions I have for you:

 

1. Do not mingle the data reading process (looping through the RecordSet to get data from each record) with AutoCAD work together. What you should do is to 

a. Create a class/structure (Type) to hold data from each record;

b. Read data to populate a collection of your custom data class;

c. after all data are available into the class collection, start AutoCAD processing work.

The reason of doing this is that AutoCAD should not be ties to how/where data is stored accessed, be it from MS Access or Excel. or Text file, or other database, or even from the cloud somewhere, as long as the data supplied is in given format/structure. Because you mention using Access, you might put you in an undesired situation where you need to install 64-bit MS Access DB Engine to every user of your code, which is quite troublesome, to say the least. Even you are OK with using Access for data access with 64-bit AutoCAD VBA, it is still good practice to separate data access code with AutoCAD working code as I mentioned above, so that later you can easily change the data source from Access to other type without having to change the code how AutoCAD does layout setup here.

 

2. While it can be done all programmatically to set up layout and viewport(s) on the layout, if I were you, I would rather create a template drawing with all possible layouts set up according to the data you stored in Access (or whatever data store). This way, AutoCAD only need to, after reading all data from data store, open new drawing, draw things, and then choose an existing layout and rename it. Hopefully, the layout can even have viewport properly set up; if not the code would do a bit viewport setup work. After all done, delete unwanted layouts. 

Doing this this way, you can easily reconfigure/update the layout setup when the design requirement/data in the data store modified for some reasons (business requirements change from time to time, right?). More importantly, with this approach, there is no need to modify the code in VBA when business requirements form layout changes.

 

 

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

parikhnidi
Advocate
Advocate

Thanks Norman for the guidance.

 

The reason for me not to set all layouts was that I have no clue how many similar components would show up. There may be two or three or even more. But I like your idea of preparing a template eliminating the requirements to for creating a fresh layout and the configure each of them in a loop. Instead, I can simply copy the layout.

 

I will work on this during break and will update you on the outcome.

 

Merry Christmas and have a wonderful holidays.

Nimish

0 Likes