
(Discuss this Article! in AUGI's Discussion Forums.)
I received this email the other day asking if it was possible to make a viewport an aec object so that when I place it the viewport is placed on the viewport layer.
This got my brain working. So I tried it. I knew that this was possible in VBA. I knew that it was also possible in AutoLISP and Visual LISP. It is certainly possible in ObjectARX, but what about the average user? Could he/she come up with a way to force the viewport on to the correct layer by simply using an AEC object inside of ADT? First things first, I knew that there was a toolbar for viewports in the AutoCAD base menu that included a tool to convert a polyline to a viewport. Could that tool be used to convert an AEC object to a viewport? Would the viewport be created on the same layer as the AEC object? Being curious, I just had to find out, so the first step was to load the AutoCAD menu and view that toolbar.
Now loading the AutoCAD base menu can be tricky in ADT and I’ve seen many a user get completely stumped when ADT commands were no longer available to them. Apparently, Architectural Desktop can get confused and then swap the ACAD menu as the main menu. This results in changes to the “File” pull down. Ever been asked where the “Export to AutoCAD” option went? It doesn’t exist on the AutoCAD menu. So in order to keep the ACAD menu loaded in your ADT session, be sure to unload any of the toolbars that are visible from the AutoCAD menu under the following conditions. There is some overlap of menus between the two products. The trick is to always look for a toolbar inside the ADT group rather than the AutoCAD group. If there is a corresponding ADT toolbar to match the AutoCAD toolbar, then the ADT version must be used.
Rather than worry about which toolbars to hide, I wrote a simple VBA macro to hide all the toolbars from the AutoCAD menu group. See Listing 1. I have this macro run automatically when one of my users loads the AutoCAD menu the first time.
Public Sub HideThemAcadToolbars()
Dim o_Toolbar As AcadToolbar
Dim o_Toolbars As AcadToolbars
Dim o_MenuGrp As AcadMenuGroup
Dim o_MenuGrps As AcadMenuGroups
Set o_MenuGrps = ThisDrawing.Application.MenuGroups
For Each o_MenuGrp In o_MenuGrps
If o_MenuGrp.Name = "ACAD" Then
Set o_Toolbars = o_MenuGrp.Toolbars
For Each o_Toolbar In o_Toolbars
If o_Toolbar.Visible Then
o_Toolbar.Visible = False
End If
Next o_Toolbar
End If
Next o_MenuGrp
End Sub
Look at the image below for the list of toolbars in the ACAD menu group that compete for ADT’s attention. Of course all this goes away in AutoCAD 2006 with the CUI running the show, but I suspected the person who asked this question wasn’t running 2006 anyway.
Now on with the testing. I created a profile in ADT 2005 and then inserted it by converting a rectangle (polyline) to a profile using the right click menu option: convert to…I also created an aec polygon, and then tried to convert this to a viewport using the built-in tool: convert object to viewport. This method did not work as the profile and polygon objects are not considered as valid candidates for conversion to viewports. So much for that good idea!
On to option two. We know that the toolbar labeled “Viewports” has tools to create viewports. I next right clicked on the toolbar and chose the customize option so that I could drag one of the viewport creation tools to a new palette I created. Once the tool appeared on the palette as shown below, I proceeded to rename it.
We are now halfway there, so I thought about what else could we use to augment this tool. We are creating a viewport, but what if we could place that viewport on the correct layer automatically? Better yet, what if we could ask the user what scale to set the viewport to, and then perform the view and lock the viewport down? Then I took a few minutes to think of how this might be created in code (LISP). See the code below for my solution.
As you can see, the code gets the value of the current layer then a LISP routine is initialized. This LISP function, ZoomQry, is defined and run each time the tool is clicked. We are also making sure that our associated arx library is loaded. We do this by loading AecLMgrLisp45.arx. Special thanks to Mark Webb of Autodesk for posting the functions contained in this file way back when in ADT 3.3. Your job is simply to ensure that the file is loaded prior to expanding the layer name. Each release gets its own name. The name shown in the code above is for ADT 2005. (Use this for ADT2006: AecLMgrLisp47.arx) I have included a dump of the text screen so that you can see this routine in action:
Command: (setvar "MENUECHO" 1)
1
Command: (defun c:ZoomQry (/) (setq DSCAL (getstring "Enter Zoom Scale <96>:
"))(if (or (= (atof DSCAL) (atof (rtos (strlen DSCAL) 2 1))) (= DSCAL 0)
(WCMATCH DSCAL ".") (wcmatch DSCAL "@") )(setq DSCAL "96"))(setq zqry (strcat
"1/" DSCAL "XP"))(princ))
C:ZOOMQRY
Command:
Command:
Command: (c:zoomqry)
Enter Zoom Scale <96>: 96
Command: (setq CLay (getvar "CLAYER"))(if (not (member "AecLMgrLisp45.arx"
(arx))) (arxload "AecLMgrLisp47"))(setq NLay (AecGenerateLayerKey
"VIEWPORT"))(setvar "CLAYER" NLay)
"G-Anno-Nplt"
Command: _LAYOUT
Enter layout option [Copy/Delete/New/Template/Rename/SAveas/Set/?] : SET
Enter layout to make current :
Command: _MVIEW
Specify corner of viewport or
[ON/OFF/Fit/Shadeplot/Lock/Object/Polygonal/Restore/2/3/4] :
Specify opposite corner: Regenerating model.
Grid too dense to display
Command:
Using these built-in tools, we are able to query the drawing for the correct layer standard and expand the layer key into a fully qualified layer name. We set this layer current, prompt the user for corners of the new viewport, then lock it down and restore the original layer. Once this code was complete, I added the new function to the tool and tried it. So far so good! I was now well on my way to creating new viewports on the correct layer and with a pre-defined scale to boot.
Please see the above code snippet for all of the code that went into this new tool. Try it yourself and see if this isn’t a productivity enhancer. To run this tool, switch to a layout tab (not model) and click the tool. You will be prompted at the command line to enter the denominator for the correct zoom scale, such as 96 for an eighth inch scale plan. Once you have identified the scale, simply pick two corners to place the viewport and watch the magic happen. I can hear you now. "What about VBA? Surely there is a better way." Yes, there is a better way!
Of course, when you are looking for a better way, there is almost always an easier method for doing something, so I tried the routine using VBA. This is your homework if you choose to accept it. Create a VBA macro that detects the addition of a viewport object and then modifies the new viewport to update the layer and viewport scale on the fly. I’ll see you next month to review your solutions. In the meantime, if you need a hint, think “events!”

(Discuss this Article! in AUGI's Discussion Forums.)
Submitted by Richard Binning, President, Local User Group Manager, and Forums Manager for AUGI Board of Directors. Richard is a member of the AEC/IS Roundtable and author of Beside The Cursor, a CAD/BIM/Technology related blog. Richard currently works as Manager, AE Technology Application, for The Haskell Company and is a Certified Autodesk Instructor for the Technology Institute of the South, a Premiere Autodesk Training Center.