Cancel
Showing results for 
Search instead for 
Did you mean: 

Conversions and Transformations using the Solid Edge API - Part 1

Gears Honored Contributor Gears Honored Contributor
Gears Honored Contributor

 

Introduction

 

Conversions from different reference systems and translations of spatial data is a common requirement in design automation using Solid Edge.

 

This series of articles discusses the following:

 

  1. Document Unit Conversion - ParseUnit and Format Unit methods of the UnitsOfMeasure object.
  2. View Coordinate Conversion - ViewToSheetViewToSheetViewToSheetViewToSheetViewToSheet and SheetToView methods of the DrawingView object.
  3. 2D to 3D Conversion - Convert2DCoordinate method of the Profile object.
  4. 3D to 2D Transformations - ModelToView method of the DrawingView object.
  5. Assembly Transformations - GetMatrix and PutMatrix methods of the Occurrence object.

 

Units of Measure

 

The following code snippet draws a triangle i.e. 3 lines with the length simply specified as 3 and height of 4.

 

Sub Main()
  Dim seApp As SolidEdgeFramework.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application")

  Dim seDoc As SolidEdgeDraft.DraftDocument = seApp.ActiveDocument
  Dim seSheet As SolidEdgeDraft.Sheet = seDoc.ActiveSheet
  
  Dim dLength As Double = 3
  Dim dHeight As Double = 4
  Dim seLine As SolidEdgeFrameworkSupport.Line2d = Nothing

  seLine = seSheet.Lines2d.AddBy2Points(0, 0, dLength, 0)
  seLine = seSheet.Lines2d.AddBy2Points(dLength, 0, dLength, dHeight)
  seLine = seSheet.Lines2d.AddBy2Points(dLength, dHeight, 0, 0)
End Sub

 

When the Draft document has its units set to mm, the triangle is drawn as seen in the image below: 

CAT02.png

 

Compare the size of the triangle with the A4 size sheet which is 297 x 210 mm.

  

The triangle appears disproportionately large because the length and height of 3 and 4 are interpreted by Solid Edge as 3 meters and 4 meters since this is the default unit of measure also called the database unit. This is irrespective of the document’s unit which could be inches or mm.

 

3 meter is 3000 mm which explains the relative sizes of the background sheet and the line lengths.

 

For drawing the lines of the correct size, the input values need to be converted to the document units without actually finding the document units separately. For this the ParseUnit method of the UnitsOfMeasure object can be used as shown in bold below:

 

Sub Main()
  Dim seApp As SolidEdgeFramework.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("SolidEdge.Application")
  Dim seDoc As SolidEdgeDraft.DraftDocument = seApp.ActiveDocument
  Dim seSheet As SolidEdgeDraft.Sheet = seDoc.ActiveSheet
 
  Dim dLength As Double = 3
  Dim dHeight As Double = 4

  Dim seUOM As SolidEdgeFramework.UnitsOfMeasure = seDoc.UnitsOfMeasure

  dLength = seUOM.ParseUnit(UnitTypeConstants.igUnitDistance, dLength)
  dHeight = seUOM.ParseUnit(UnitTypeConstants.igUnitDistance, dHeight)

  Dim seLine As SolidEdgeFrameworkSupport.Line2d = Nothing
  seLine = seSheet.Lines2d.AddBy2Points(0, 0, dLength, 0)
  seLine = seSheet.Lines2d.AddBy2Points(dLength, 0, dLength, dHeight)
  seLine = seSheet.Lines2d.AddBy2Points(dLength, dHeight, 0, 0)
End Sub

 

Now when the lines are drawn, selecting one of them displays its length in the Command bar which is correctly displayed as that specified in the code.

 

CAT03.png

 

Similarly, when you read values or distance measurements from Solid Edge using the API methods, they are returned in default unit which is meters. To convert it to a display unit use the FormatUnit method.

 

For example, here's a piece of code adapted from @JasonTitcomb's EyeDropper for Solid Edge which displays the length of an edge in the status bar when the mouse hovers over an edge of a Part or Sheetmetal model.

 

Imports System.Runtime.InteropServices
Imports SolidEdgeFramework

Module Module1
  Private mSolidApp As SolidEdgeFramework.Application = Nothing
  Private mCommand As Command = Nothing
  Private mMouse As Mouse = Nothing
  Private seUoM As UnitsOfMeasure = Nothing

  Public Sub main()
    mSolidApp = TryCast(Marshal.GetActiveObject("SolidEdge.Application"), SolidEdgeFramework.Application)

    seUoM = mSolidApp.ActiveDocument.UnitsOfMeasure
    mCommand = mSolidApp.CreateCommand(SolidEdgeConstants.seCmdFlag.seNoDeactivate)

    AddHandler mCommand.Terminate, AddressOf command_Terminate
    mCommand.Start()
    mMouse = mCommand.Mouse

    With mMouse
      .LocateMode = 1
      .WindowTypes = 1
      .EnabledMove = True
      .AddToLocateFilter(SolidEdgeConstants.seLocateFilterConstants.seLocateEdge)

      AddHandler .MouseMove, AddressOf mouse_MouseMove
End With

    System.Windows.Forms.Application.Run()
End Sub

Private Sub mouse_MouseMove(ByVal sButton As Short, ByVal sShift As Short, ByVal dX As Double, ByVal dY As Double, ByVal dZ As Double, ByVal pWindowDispatch As Object, ByVal lKeyPointType As Integer, ByVal pGraphicDispatch As Object)
  Dim theEdge As SolidEdgeGeometry.Edge = DirectCast(pWindowDispatch, SolidEdgeGeometry.Edge)
  Dim EdgeLength As Double, MinParam As Double, MaxParam As Double

  If theEdge IsNot Nothing Then
    theEdge.GetParamExtents(MinParam, MaxParam)
    theEdge.GetLengthAtParam(MinParam, MaxParam, EdgeLength)
    EdgeLength = seUoM.FormatUnit(UnitTypeConstants.igUnitDistance, EdgeLength, PrecisionConstant:=SolidEdgeConstants.PrecisionConstants.igPrecisionThousandths)

    mSolidApp.StatusBar = EdgeLength.ToString
  End If
End Sub

 

Without the FormatUnit method of the UnitsOfMeasure object, the value displayed in the status bar is in meter irrespective of the unit of the document.

 

 

CAT06.png

 

When FormatUnit is used, the same program displays the length of the edge in the document unit without any changes or conversion.

 

In the next part of this series, I will show you in depth how to convert coordinates of a point on the drawing sheet to the window to create a brand new command called Zoom to Selection.

 

Meanwhile, if you have any questions regarding this article, kindly post them on the Solid Edge Developer Forum.

 

Tushar Suradkar

www.CADVertex.com

www.SurfAndCode.IN

 

Solid Edge User Group on FaceBook:

https://www.facebook.com/groups/solidedgeusers/

Comments
Gears Phenom

@Tushar    As always - Great work!