{"id":2121,"date":"2019-05-16T08:36:05","date_gmt":"2019-05-16T15:36:05","guid":{"rendered":"https:\/\/blogs.plm.automation.siemens.com\/t5\/Solid-Edge-Blog\/Conversions-and-Transformations-using-the-Solid-Edge-API-Part-1\/ba-p\/594024"},"modified":"2020-02-17T09:42:32","modified_gmt":"2020-02-17T14:42:32","slug":"conversions-and-transformations-using-the-solid-edge-api-part-1","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/conversions-and-transformations-using-the-solid-edge-api-part-1\/","title":{"rendered":"Conversions and Transformations using the Solid Edge API &#8211; Part 1"},"content":{"rendered":"<p><P><STRONG>Introduction<\/STRONG><\/P><\/p>\n<p><P>Conversions from different reference systems and translations of spatial data is a common requirement in design automation using Solid Edge.<\/P><\/p>\n<p><P>This series of articles discusses the following:<\/P><\/p>\n<p><OL><br \/>\n<LI><U>Document Unit Conversion<\/U> &#8211; ParseUnit and Format Unit methods of the UnitsOfMeasure object.<\/LI><br \/>\n<LI><U>View Coordinate Conversion<\/U> &#8211; ViewToSheet and SheetToView methods of the DrawingView object.<\/LI><br \/>\n<LI><U>2D to 3D Conversion<\/U> &#8211; Convert2DCoordinate method of the Profile object.<\/LI><br \/>\n<LI><U>3D to 2D Transformations<\/U> &#8211; ModelToView method of the DrawingView object.<\/LI><br \/>\n<LI><U>Assembly Transformations<\/U> &#8211; GetMatrix and PutMatrix methods of the Occurrence object.<\/LI><br \/>\n<\/OL><\/p>\n<p><P><STRONG>Units of Measure<\/STRONG><\/P><\/p>\n<p><P>The following code snippet draws a triangle i.e. 3 lines with the length simply specified as 3 and height of 4.<\/P><\/p>\n<p><PRE>Sub Main()<br \/>\n  Dim seApp As SolidEdgeFramework.Application = System.Runtime.InteropServices.Marshal.GetActiveObject(&#8220;SolidEdge.Application&#8221;)<\/p>\n<p>  Dim seDoc As SolidEdgeDraft.DraftDocument = seApp.ActiveDocument<br \/>\n&nbsp; Dim seSheet As SolidEdgeDraft.Sheet = seDoc.ActiveSheet<br \/>\n&nbsp;<br \/>\n  Dim dLength As Double = 3<br \/>\n&nbsp; Dim dHeight As Double = 4<br \/>\n&nbsp; Dim seLine As SolidEdgeFrameworkSupport.Line2d = Nothing<\/p>\n<p>&nbsp; seLine = seSheet.Lines2d.AddBy2Points(0, 0, dLength, 0)<br \/>\n&nbsp; seLine = seSheet.Lines2d.AddBy2Points(dLength, 0, dLength, dHeight)<br \/>\n&nbsp;&nbsp;seLine = seSheet.Lines2d.AddBy2Points(dLength, dHeight, 0, 0)<br \/>\nEnd Sub<\/PRE><\/p>\n<p><P>When the Draft document has its units set to mm, the triangle is drawn as seen in the image below:&nbsp;<\/P><br \/>\n<P><span class=\"lia-inline-image-display-wrapper lia-image-align-center\" style=\"width: 259px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/CAT02-1.png\" alt=\"CAT02.png\" title=\"CAT02.png\" \/><\/span><\/P><\/p>\n<p><P>Compare the size of the triangle with the A4 size sheet which is 297 x 210 mm.<\/P><br \/>\n<P>&nbsp;&nbsp;<\/P><br \/>\n<P>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\u2019s unit which could be inches or mm.<\/P><\/p>\n<p><P>3 meter is 3000 mm which explains the relative sizes of the background sheet and the line lengths.<\/P><\/p>\n<p><P>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:<\/P><\/p>\n<p><PRE>Sub Main()<br \/>\n&nbsp; Dim seApp As SolidEdgeFramework.Application = System.Runtime.InteropServices.Marshal.GetActiveObject(&#8220;SolidEdge.Application&#8221;)<br \/>\n  Dim seDoc As SolidEdgeDraft.DraftDocument = seApp.ActiveDocument<br \/>\n  Dim seSheet As SolidEdgeDraft.Sheet = seDoc.ActiveSheet<br \/>\n&nbsp;<br \/>\n  Dim dLength As Double = 3<br \/>\n&nbsp;&nbsp;Dim dHeight As Double = 4<\/p>\n<p>&nbsp; <STRONG>Dim seUOM As SolidEdgeFramework.UnitsOfMeasure = seDoc.UnitsOfMeasure<\/STRONG><\/p>\n<p> <STRONG> dLength = seUOM.ParseUnit(UnitTypeConstants.igUnitDistance, dLength)<br \/>\n&nbsp;&nbsp;dHeight = seUOM.ParseUnit(UnitTypeConstants.igUnitDistance, dHeight)<\/STRONG><\/p>\n<p>&nbsp; Dim seLine As SolidEdgeFrameworkSupport.Line2d = Nothing<br \/>\n&nbsp; seLine = seSheet.Lines2d.AddBy2Points(0, 0, dLength, 0)<br \/>\n&nbsp; seLine = seSheet.Lines2d.AddBy2Points(dLength, 0, dLength, dHeight)<br \/>\n&nbsp; seLine = seSheet.Lines2d.AddBy2Points(dLength, dHeight, 0, 0)<br \/>\nEnd Sub<\/PRE><\/p>\n<p><P>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.<\/P><\/p>\n<p><P><span class=\"lia-inline-image-display-wrapper lia-image-align-center\" style=\"width: 431px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/CAT03-1.png\" alt=\"CAT03.png\" title=\"CAT03.png\" \/><\/span><\/P><\/p>\n<p><P>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.<\/P><\/p>\n<p><P>For example, here&#8217;s a piece of code adapted from <a href=\"https:\/\/blogs.plm.automation.siemens.com\/t5\/user\/viewprofilepage\/user-id\/4059\" target=\"_blank\" rel=\"noopener\">@JasonTitcomb<\/a>&#8216;s <a href=\"https:\/\/archive.codeplex.com\/?p=facestyleeyedropperforsolidedge\" target=\"_blank\" rel=\"noopener nofollow noreferrer\">EyeDropper for Solid Edge<\/A> which displays the length of an edge in the status bar when the mouse hovers over an edge of a Part or Sheetmetal model.<\/P><\/p>\n<p><PRE>Imports System.Runtime.InteropServices<br \/>\nImports SolidEdgeFramework<\/p>\n<p>Module Module1<br \/>\n  Private mSolidApp As SolidEdgeFramework.Application = Nothing<br \/>\n&nbsp;&nbsp;Private mCommand As Command = Nothing<br \/>\n&nbsp;&nbsp;Private mMouse As Mouse = Nothing<br \/>\n&nbsp;&nbsp;<STRONG>Private seUoM As UnitsOfMeasure = Nothing<\/STRONG><\/p>\n<p>&nbsp;&nbsp;Public Sub main()<br \/>\n&nbsp;&nbsp;  mSolidApp = TryCast(Marshal.GetActiveObject(&#8220;SolidEdge.Application&#8221;), SolidEdgeFramework.Application)<\/p>\n<p>    <STRONG>seUoM = mSolidApp.ActiveDocument.UnitsOfMeasure<\/STRONG><br \/>\n    mCommand = mSolidApp.CreateCommand(SolidEdgeConstants.seCmdFlag.seNoDeactivate)<\/p>\n<p>    AddHandler mCommand.Terminate, AddressOf command_Terminate<br \/>\n    mCommand.Start()<br \/>\n    mMouse = mCommand.Mouse<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;With mMouse<br \/>\n      .LocateMode = 1<br \/>\n      .WindowTypes = 1<br \/>\n      .EnabledMove = True<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.AddToLocateFilter(SolidEdgeConstants.seLocateFilterConstants.seLocateEdge)<\/p>\n<p>      AddHandler .MouseMove, AddressOf mouse_MouseMove<br \/>\nEnd With<\/p>\n<p>    System.Windows.Forms.Application.Run()<br \/>\nEnd Sub<\/p>\n<p>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)<br \/>\n  Dim theEdge As SolidEdgeGeometry.Edge = DirectCast(pWindowDispatch, SolidEdgeGeometry.Edge)<br \/>\n  Dim EdgeLength As Double, MinParam As Double, MaxParam As Double<\/p>\n<p>&nbsp;&nbsp;If theEdge IsNot Nothing Then<br \/>\n    theEdge.GetParamExtents(MinParam, MaxParam)<br \/>\n    theEdge.GetLengthAtParam(MinParam, MaxParam, EdgeLength)<br \/>\n    <STRONG>EdgeLength = seUoM.FormatUnit(UnitTypeConstants.igUnitDistance, EdgeLength, PrecisionConstant:=SolidEdgeConstants.PrecisionConstants.igPrecisionThousandths)<\/STRONG><\/p>\n<p>    mSolidApp.StatusBar = EdgeLength.ToString<br \/>\n&nbsp;&nbsp;End If<br \/>\nEnd Sub<\/PRE><\/p>\n<p><P>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.<\/P><\/p>\n<p><P><span class=\"lia-inline-image-display-wrapper lia-image-align-center\" style=\"width: 571px;\"><img decoding=\"async\" src=\"http:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/CAT06-1.png\" alt=\"CAT06.png\" title=\"CAT06.png\" \/><\/span><\/P><\/p>\n<p><P>When FormatUnit is used, the same program displays the length of the edge in the document unit without any changes or conversion.<\/P><\/p>\n<p><P>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.<\/P><\/p>\n<p><P>Meanwhile, if you have any questions regarding this article, kindly post them on the <a href=\"http:\/\/community.plm.automation.siemens.com\/t5\/Solid-Edge-Developer-Forum\/bd-p\/SEDeveloperForum\" target=\"_blank\" rel=\"noopener nofollow noreferrer\">Solid Edge Developer Forum<\/A>.<\/P><\/p>\n<p><P><SPAN>Tushar Suradkar<\/SPAN><\/P><br \/>\n<P><SPAN><a href=\"http:\/\/www.cadvertex.com\/\" target=\"_blank\" rel=\"noopener nofollow noreferrer\">www.CADVertex.com<\/A><\/SPAN><\/P><br \/>\n<P><a href=\"http:\/\/www.surfandcode.in\/2014\/01\/index-of-all-tutorials-on-this-solid.html\" target=\"_blank\" rel=\"noopener nofollow noreferrer\"><SPAN>www.SurfAndCode.IN<\/SPAN><\/A><\/P><\/p>\n<p><P>Solid Edge User Group on FaceBook:<\/P><br \/>\n<P><a href=\"https:\/\/www.facebook.com\/groups\/solidedgeusers\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">https:\/\/www.facebook.com\/groups\/solidedgeusers\/<\/A><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;<br \/>\n Introduction <\/p>\n<p> Conversions from different reference systems and translations of spatial data is a common requirement in design automation using Solid Edge. <\/p>\n<p> This series of&#8230;<\/p>\n","protected":false},"author":42979,"featured_media":2131,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1,96],"tags":[],"industry":[],"product":[],"coauthors":[],"class_list":["post-2121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","category-tips-tricks"],"featured_image_url":"https:\/\/blogs.stage.sw.siemens.com\/wp-content\/uploads\/sites\/8\/2019\/09\/CAT06-1.png","_links":{"self":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts\/2121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/users\/42979"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/comments?post=2121"}],"version-history":[{"count":4,"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts\/2121\/revisions"}],"predecessor-version":[{"id":2132,"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/posts\/2121\/revisions\/2132"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/media\/2131"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/media?parent=2121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/categories?post=2121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/tags?post=2121"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/industry?post=2121"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/product?post=2121"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/solidedge\/wp-json\/wp\/v2\/coauthors?post=2121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}