Adding a cell to drawings via VBA

Hi,

Im new to coding in Microstation, have a bit of VBA AutoCAD experience. Im trying to insert a cell into a DGN via VBA (have many todo).

I have attached a cell library via "AttachCellLibrary". When trying to set the active cell I get "Unknown error"

CadInputQueue.SendCommand "PLACE CELL ICON"     'insert active cell

This has been cobbled together from a recorded macro and a bit of playing around. 

Sub AddRevBlock()
Dim startPoint As Point3d
Dim point As Point3d, point2 As Point3d
Dim CellLib As String
Dim DGNSize As String

CadInputQueue.SendCommand "AS=1" 'set active scale to 1
CadInputQueue.SendCommand "AA=0" 'set active rotation to 0

DGNSize = GetTitleSize 'get dgn size

' Start a command
CadInputQueue.SendCommand "DIALOG CELLMAINTENANCE TOGGLE" ' not sure what this is for, MS macro recording added it

'attach the standard cell lib
CellLib = "D:\Microstation\RevBlock.CEL"
AttachCellLibrary CellLib
'Call AttachedCellLib

ActiveSettings.CellName =  "REV Block"

CadInputQueue.SendCommand "PLACE CELL ICON" 'insert active cell

' Send a data point to the current command
point.X = startPoint.X
point.Y = startPoint.Y
point.Z = startPoint.Z
CadInputQueue.SendDataPoint point, 1

' Send a reset to the current command

CadInputQueue.SendReset
CommandState.StartDefaultCommand
End Sub

Parents
  • Hi John,

    some comments and advises:

    • Please be aware this is general Developers and Programming forum, whereas your question is about MicroStation programming. I recommend to move the question to MicroStation Programming forum. To move existing post to another forum, use More > Move tool available under your original post.
    • I also recommend to read and follow the best practices, especially:
      • Use standardized subject format [<product> <version> <language>] Topic (e.g. [MStn CE U14.2] How to add cell to model?. It helps to share mandatory information in simple way (e.g. in your question, information about version is missing).
      • Use Insert > Insert code tool to share any code snippet (even one line). To post the code as plain text makes the post unreadable.
    have a bit of VBA AutoCAD experience

    I do not have experience with AutoCAD VBA, but because some concepts are quite different in MicroStation and AutoCAD, VBA API is different also.

    This has been cobbled together from a recorded macro and a bit of playing around. 

    Even when VBA macro recorder is helpful often, for programming it's not recommended tool. A problem is that the created code is not good VBA code, but in fact a script of user's action, transformed to code that can be used as VBA.

    To simulate user's action is not a good way and should be used only when it's really necessary, because e.g. such functionality is missing in VBA API. But usually, to simulate users lead to worse and unstable code.

    VBA API offers rich functionality that allows to access MicroStation functionality directly.

    I have attached a cell library via "AttachCellLibrary".

    In MicroStation it's not required to attach cell library before a cell is placed. It's recommended to use MS_CELLLIST variable instead of attaching a library.

    But when you preferred to attach the library, use AttachCellLibrary method from VBA API.

    Im trying to insert a cell into a DGN via VBA

    It's not clear whether you want to place the cell dynamically (similarly to Place cell tool) or simply to place it at some coordinates without an interaction with user. Anyway, the core method here is CreateCellElement1 method.

    Im new to coding in Microstation

    I recommend to start with:

    • Examples in MicroStation VBA help
    • VBA macros (examples) delivered with MicroStation installation
    • Search MicroStation Programming forum for discussed topic (e.g. "vba place cell" phrase returns a lot of results)
    • Also wiki contains some examples (e.g. this one)
    • There is, quite old but still valid and useful, Learning MicroStation VBA book, available both from Bentley and in some bookstores like Amazon.
    • I guess it's possible to find some VBA courses on Bentley LEARN server.

    With regards,

      Jan

  • Thanks Jan,

    I will have a read of the linked documents and go from there. The code is working at the moment, to dynamically place a cell in multiple drawings at various set locations i.e. different for A2 and A3 drawings. Some drawings have been scaled, or not set at 0,0,0 (working with Client drawings of varying quality)

    I now need to read the existing drawing border scale and insertion point, to adjust the cell insertion point and location based on the existing title block.

    I will do a bit of reading, this should be relatively straight forward.

Reply
  • Thanks Jan,

    I will have a read of the linked documents and go from there. The code is working at the moment, to dynamically place a cell in multiple drawings at various set locations i.e. different for A2 and A3 drawings. Some drawings have been scaled, or not set at 0,0,0 (working with Client drawings of varying quality)

    I now need to read the existing drawing border scale and insertion point, to adjust the cell insertion point and location based on the existing title block.

    I will do a bit of reading, this should be relatively straight forward.

Children
No Data