Transcription of MicroStation V8 and VBA - bentleyuser.dk
1 ControlAltDelete 26 Fourth Quarter 2001 Feature Feature Feature Feature MicroStation V8 and VBAM icroStation V8 and VBAM icroStation V8 and VBAM icroStation V8 and VBAby Mark Stefanchuk, recent release of MicroStation V8 introducesanother automation feature, visual Basic for applications (VBA). This new implementation of VBA provides rapiddevelopment features for designing applications and toolsfor can leverage built in knowledge ofWindows programming to improve the CAD workflowand environment. Casual developers, such as CAD man-agers, can take advantage of visual interface constructiontools to build Windows like programs these advantages however, CAD managersmay still opt to defer development of more sophisticatedapplications to professional programmers.
2 Although VBAmakes automation easier, there are still problems thatrequire programming skill and experience. Compared tothe alternatives, VBA is still , a C programming language has always had anadvantage over MicroStation BASIC Macros for a coupleof reasons. First, MDL creates elements dynamically displayingas they are dragged around the screen. And second, macros must be started each time youneed them, where as MDL applications are loaded,but run silent until a command is requested. How-ever, MDL requires considerable programming exper-tise, which usually is outside of the average usersscope of VBA will run until requested, can han-dle complex dynamics, so building applications in thiseasy to use programming environment sounds like the wayto proceed. But, is ease of programming really the mostimportant aspect of VBA?
3 Well, maybe. But, considering that MDL, Macros,and don t forget JMDL can already handle automationneeds do we need VBA?In a word, yes. VBA has at least one more has the ability to read and write to MicroStation fromother applications . For example, a VBA application in Excel can scanthe contents of a MicroStation file directly and manipulatethe spreadsheet or the design file based on conditions dis-covered in either manufacturer of spray nozzles may use a spread-sheet to calculate the flow rate required in a main supply-ing a nozzle array. The spreadsheet could be arranged suchthat the total flow rate and the pipe main size are calcu-lated based on nozzle model numbers entered into thespreadsheet. An Excel VBA could automatically draw the arrayand label the drawing with size and flow parameters of thearray. The spreadsheet user doesn t need to open theMicroStation drawing or even know how to use 1 Figure 1 Figure 1F igure 1 Create Drawing From Excel VBAIn the next few pages we will examine the mechanicsof the MicroStation VBA process.
4 Using a less complexexample we will build an Excel VBA to extract cell namesfrom a design file and lists the names in the course this is a simple example, but it can beextended to create a quantity take off application, or anequipment schedule tool. The possibilities are linking specification documents to schedules, orcomparing vendor drawing data to design data in yourMicroStation file. Fourth Quarter 2001 27 ControlAltDelete What? Your vendor drawings are in AutoCAD for-mat not MicroStation ? No problem, AutoCAD imple-ments VBA too, so use MicroStation VBA applications toread data from the AutoCAD Excel VBA about MicroStationTeaching Excel VBA about MicroStationTeaching Excel VBA about MicroStationTeaching Excel VBA about MicroStationBefore you can reference MicroStation objects inExcel, VBA needs to know that MicroStation exists.
5 Wedo this by creating a reference to the DGN the following the VBA editor in the application you want to reference MicroStation from (Excel the keyboard shortcut Alt-F11 Alt-F11 Alt-F11 Alt-F11 will open the editor.)2 Under the Tools pull down menu, choose References. A dialog appears with a list of references for your 2 Figure 2 Figure 2F igure 2 Object Library with Small Box Checked3 Scroll through this list to find Bentley MicroStation DGN Object Library 4 Check the small box next to Bentley MicroStation DGN Object Library and then click OkOkOkOk on the References that there is an object Library we no longerneed to create an OLE application object. Some of you may recall that using createObject inVB 6 to edit MicroStation . msApp = createObject( )Creating an InterfaceCreating an InterfaceCreating an InterfaceCreating an Interface Add a form to the VBA by choosing the Pull DownMenu Insert UserForm.
6 Figure 3 Figure 3 Figure 3F igure 3 Tools UserForm Pull Down Menu Place a Button on the Form and change the Caption to Get Cell Names .Figure 4 Figure 4 Figure 4F igure 4 Add Button Get Cell Names to UserForm1 Open Excel, pr ess Alt-F11 Open Excel, pr ess Alt-F11 Open Excel, pr ess Alt-F11 Open Excel, pr ess Alt-F11 Drag button into formthen rename boxTipsTipsTipsTipsConverting Raster to VectorCan I convert a raster file to a vector file in micro-station J?-------------------------------------- ----------------------You need the module MicroStation Descartes orIRAS/B to the job. CADCADCADCADC ontrolAltDelete 28 Fourth Quarter 2001 Building the CommandBuilding the CommandBuilding the CommandBuilding the Command Double click on the Get Cell Names button to openthe source code window (see Figure 5).
7 A template sub routine is automatically created. Itwill look like Sub CommandButton1_Click()End SubFigure 5 Figure 5 Figure 5F igure 5 Source Code WindowInside this subroutine we will place the script thatopens a file, scans the cells in the file, reads the cell namesand then places the names into the spreadsheet file. Here swhat it will look Sub CommandButton1_Click()Dim oCell As CellElementDim oScanCriteria As ElementScanCriteriaDim dFile As DesignFileDim curCell As Object'OPEN THE DESIGN FILESet dFile = OpenDesignFile("c:\temp\ ", True)Set oScanCriteria = New ElementScanCriteria' SET THE SCAN msdElementTypeCellHeaderDim oScanEnumerator As ElementEnumeratorSet oScanEnumerator = (oScanCriteria)i = 1Do While oCell = curCell = Worksheets(1).Cells(i, 1) = = i + SubAfter opening the design file, a scan criteria is of this as a filter. In this case we only want to lookfor cells.
8 The scan enumerator can be thought of as a list ofelements that the search finds, which is executed by the elements are loaded into the scan enumera-tor (list). Each element can be processed. In this examplewe use a do while condition to step through each code that populates the spreadsheet is containedinside the do while and is just two lines which sets thevalue of the spreadsheet cell (careful, it s just a row-col-umn location in the spreadsheet, and not the same as aMicroStation cell.) to the name of the MicroStation cellfound by the the ApplicationRunning the ApplicationRunning the ApplicationRunning the ApplicationA Workbook sheets can also contain buttons. In Fig-ure 6, the spreadsheet shows a button labeled Get CellTool .Figure 6 Figure 6 Figure 6F igure 6 Sheet containing there a program that will excute the "copy par-allel by distance" command, then change that newlycopied element to the active level & symbology, all atonce?
9 ---------------------------------------- --------------------For a Copy Parallel With Active , is the BASIC macro. Check n/fr eed wn l/cp wat t sp C AD/fr eed wn l/cp wat t sp C AD/fr eed wn l/cp wat t sp C AD/fr eed wn l/cp wat t sp C ADQuickie TipQuickie TipQuickie TipQuickie TipWhat is a file in MicroStation ?--------------------------- ---------------------------------It is an Bentley internal system file related tolicensing. Users need not to know about it. C ADCADCADCADF ourth Quarter 2001 29 ControlAltDeleteThis button also has a code subroutine. The subrou-tine opens the UserForm1. The code to open the formlooks like Sub CommandButton1_Click()Load SubTo access place buttons on sheets and access the codewindow for these buttons, click on Design Mode.
10 This isthe triangle icon located on the Controls Toolbox(View> Toolbars).F igure 7 F igure 7 F igure 7 F igure 7 Design Mode Toggle on Control Tools ToolboxOnce the button has been placed and you add thecode to the button, Exit Design Mode by clicking on thedesign mode button in the Control Tools toolbox again. Now run the application by clicking on the sheet but-ton. UserForm1 will display. Click the Get Cell Names button. Column 1 is popu-lated with cell CommentsFinal CommentsFinal CommentsFinal CommentsIn designing UserForm1 it would have been appro-priate to include a text box to prompt the user for the loca-tion of the design file. Further, a browse button helps theuser search for a file. Do you think you could add these toyour form? Try it. The completed code is available 8 Figure 8 Figure 8F igure 8 Form with File Text Box, Browse ,Cancel ButtonVBA opens up new tool kit for office MicroStation objects in this way can lead tomany new applications .