I found several links discussing viewports and layouts and found it a bit confusing. I managed to get what I wanted but am left with some questions. We are planning for a base coordinate system change which means all of our CAD data will move. The ultimate goal is to check scales, then pan and zoom each floating modelspace viewport as required, in potentially 1000s of dwg files.
The dwg file I am testing has 45 layouts and 1 viewport on each. The code attached is what I used to get the numbers to match. I understand that viewport 1 on the layout is the paperspace layout itself. But, I had several viewport numbers of -1 on each layout. I thought -1 represented an empty list, like nothing selected in a listbox.
1 - What does viewport number -1 mean?
2 - What is the purpose of viewporttable?
3 - Why doesn't viewporttable contain viewporttablerecords that have this information - or does it?
There might be more later - now I need a vacation.
Solved! Go to Solution.
I've never noticed the viewport number of -1 before until trying your code, looks to me like that number represents a non-current layout, maybe someone knows otherwise? It looks like the current layout will give a positive number and non-current will give a number of -1.
The ViewportTable holds ViewportTableRecord objects which are the tiled viewports in model space.
Viewport objects are the floating viewports on a layout.
From the ObjectARX Reference Guide ->AcDbViewport::number:
This function returns the viewport ID number. This is the number that is reported by the AutoCAD CVPORT system variable when the viewport is the current viewport in the AutoCAD editor. If the viewport is inactive, -1 is returned.
This value is not saved with the drawing, and changes each time the drawing is opened.
The viewport ID number is used for DXF group code 69.
I have a drawing with 6 layouts with 1 model space viewport on each. 1 of the layouts has a viewport that has not been activated (whoever created the drawing never switched to model space in this layout). Because of this, there is no viewport number > 1 returned by layout.getviewports(). If I manually activate model space on that layout, and run my code again, it finds the viewport. This means I have a drawing with a layout with a viewport that my code won't find.
Is there another way to find all of the viewports and make sure model space has been activated?
In a layout's viewport collection, the first viewport is actually the screen you are looking at. The other viewports are what you created using Mview command inside of autocad. If you mess with the first viewport object you will get some very funny problems with your cad file (delete one and see!).
My appologies, all my text got trashed when I posted my last.
Try the attached code that I posted.
Sounds like the 'Layout' has never been switched to/activated
You can use the Layout.Initialize() method.
It will work for a layout that has not been initialized, but will throw an exception for one that has been initialized.
This is what it states in the ObjectARX docs for the AcDbLayout.initialized() method:
This method will do the behind the scenes work to establish a Paper Space viewport for the layout. This allows a layout to be set up and made ready for other viewports and entities to be put into the layout as well as to set various layout properties.
Returns Acad::eOk if successful; otherwise, returns an AutoCAD error status.
So just calling Initialize() on a Layout object won't completely activate it like manually switching to it would.
You shouldn't have to switch to 'model space' thru a Viewport to active the viewport though and have it initialized, hope I understand that part correctly in your post.
Hope this helps!
Thanks to all contributors. I did not notice or consider the initialize method of a layout. I will post my solution when finished. I have to move on for now.
Working with the APIs for autocad and autocad map reminds me of playing with a house of cards - be careful of which card you pull and how you put it back.
Attached is the code and a DWG file that shows some of the problems I am finding. There are 8 layouts (l1-l8). l1 through l3 look normal, except that properties shows 1 polyline and 1 viewport when I select all, but there is only 1 object. But, l4 through l8 are causing trouble. The code results in 15 layouts on l4, but I can only see 1 in Autocad.
Can someone explain what I am seeing here and how to work around it? I am going to check viewport centers and sizes to see if they make sense - I haven't tried that yet.