Tag Archive | VBA

Cell Rotation – MicroStation VBA

Want the rotation of a cell about the Z-axis? This is how I get it using the Bentley object model. 

Public Function getCellRotation(oCell As CellElement) As Double
    Dim rAngle As Double
    getCellRotation = 0
    If (Matrix3dIsXYRotation(oCell.Rotation, rAngle)) Then
        getCellRotation = Degrees(rAngle)
        Debug.Print Trim(Str(Format(Degrees(rAngle), "#0.0")))
    End If
End Function

Matrix3dIsXYRotation passes the rotation matrix to the method and then sends back the angle (in radians) back to rAngle. The method returns true when there is a rotation about Z. If the cell is skewed or mirrored the method will return false. Be careful not to assume that if the function returns false that there is 0 rotation. You can make this assumption if your application controls the orientation of the cell and you can then predict that cells won’t be skewed or mirrored.

VBA Shortcut Methods

Bentley Systems does a great job of making MicroStation VBA easy. With each new release of the object model there are new objects, methods, and properties.Most support new functionality, but in some cases these additions help improve readability. For example,

GetLastValidGraphicalElement is a shortcut for GraphicalElementCache.GetLastValidElement

I recommend checking the Remarks section of the help documentation and the version for the methods you use. Especially if you are writting VBAs for older V8 releases. You may have to use the long form instead of the shortcut if you are writting a macro for an older version of MicroStation. I’ve also noticed that the documents don’t provide the precise release versions. So even though the new method was implemented in V8 2004, it may not have been added until a later release of V8 2004. In this case replace the shortcut with the long form of the method.

Straighten Line

I received an email recently from a reader who asked how to straighten lines? Given a bunch of lines placed at different angles to one another how would you make them parrallel and all the same length? Something like this.

straighten lines

The interesting bit here isn't how do you straighten the lines (just match end points, remove the original and draw a new line) but how do you determine which line is the template?

From a developer's point of view these are design objectives and not programming issues. In the example I created I decided to use the first line found in the selection set or fence. In reality the designer might need to use any number of lines as the template. In this case you will need to first identify the template line before running the processor. I would call this a business rule, or design rule and it creates a requirement that removes autonomy from the program. In my program I ask, am I processing the first line. If yes, then I save the end points and if no, it means I already have the end points so just process.  In MicroStation VBA (a good way to prototype) it might look like this…

Complete code can be downloaded from the cadgurus page. Filter by MVBA and look for xy_change_vert (xy_change_vert.zip).

Easy. This means the program is deciding which line to use for me. Notice that this program also provides an option so that the designer can decide which line should be the template. It says if the use first element option is turned off then the program expects to find coordinate values in the form. Another approach might be to build a locate command. In fact this might prove to be better since you won't need to validate the form controls (ie make sure points have been defined).

Homework: Find the source file in the cadgurus archive. Study the locate command and figure out how to eliminate the form. Advanced: Can you validate that the element selected is a line element?

Getting Started

I was working my way (hacking) through a new, well at least new to me, programming language when it occurred to me that the information I was finding didn’t start from scratch. It made me think, “if I were a CAD Manager who needed to automate something, and had never coded, where would I start?”

Those of us trained as designers, engineers, and architects versus computer programmers probably started with some example code. Nope, back further, we probably wondered what to do with this cryptic file of instructions. I know I can edit this in notepad, but then what?  How do I run it? Stepwise instruction often assumes you know the basics.

So, newbies, now that I got you all interested I’m going to tell you that we’re probably not going to resolve that – at least not in one post.  What I can offer is a link to an old article I wrote many years ago – to get you started – “The Five Minute VBA“. It’s still relevant and can help you get started if you are a new CAD manager and/or are new to CAD development.