Hide Table of Contents

Transform Coordinates from Sketch to Model Space Example (VBA)

When an entity is selected while editing a sketch, for example a sketch point, the coordinates of the point are shown in the space of the sketch. Sometimes it is desirable to know the coordinates of this point in the space of the model.

This example shows how to use various transforms and math utilities to transform a point in sketch space to model space.

 

'------------------------------------------------------------------

'

' Preconditions:

'       (1) Part, assembly or drawing is open.

'       (2) If a part or assembly, then a sketch is being edited.

'       (3) If a part or assembly, then an entity is selected in

'           the sketch.

'       (4) If a drawing, then an entity is selected.

'

' Postconditions: None

'

' NOTES:

'       (1) If the sketch is a 3D sketch, then the selected sketch

'           point is automatically in model coordinates.

'       (2) If the sketch is a 3D sketch, then its transform is the

'           unit transform.

'

'------------------------------------------------------------------

Option Explicit

Public Function GetModelCoordinates _

( _

    swApp As SldWorks.SldWorks, _

    swSketch As SldWorks.sketch, _

    vPtArr As Variant _

) As Variant

    Dim swMathPt                    As SldWorks.MathPoint

    Dim swMathUtil                  As SldWorks.MathUtility

    Dim swMathTrans                 As SldWorks.MathTransform

    Set swMathUtil = swApp.GetMathUtility

    Set swMathPt = swMathUtil.CreatePoint(vPtArr)

    ' Is a unit transform if 3D sketch; for example, selected sketch

    ' point is automatically in model space

    Set swMathTrans = swSketch.ModelToSketchTransform

    Set swMathTrans = swMathTrans.Inverse

    Set swMathPt = swMathPt.MultiplyTransform(swMathTrans)

    GetModelCoordinates = swMathPt.ArrayData

End Function

Sub main()

    Dim swApp                       As SldWorks.SldWorks

    Dim swModel                     As SldWorks.ModelDoc2

    Dim swSelMgr                    As SldWorks.SelectionMgr

    Dim swSketch                    As SldWorks.sketch

    Dim vSketchSelPt                As Variant

    Dim vModelSelPt                 As Variant

    Dim i                           As Long

    

    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

    Set swSketch = swModel.GetActiveSketch2

    

    vSketchSelPt = swSelMgr.GetSelectionPointInSketchSpace(1)

    vModelSelPt = GetModelCoordinates(swApp, swSketch, vSketchSelPt)

    

    Debug.Print "File = " & swModel.GetPathName

    Debug.Print "  Is3D sketch          = " & swSketch.Is3D

    Debug.Print "  SelPt (sketch space) = (" & vSketchSelPt(0) * 1000# & ", " & vSketchSelPt(1) * 1000# & ", " & vSketchSelPt(2) * 1000# & ") mm"

    Debug.Print "  SelPt (model  space) = (" & vModelSelPt(0) * 1000# & ", " & vModelSelPt(1) * 1000# & ", " & vModelSelPt(2) * 1000# & ") mm"

End Sub

'------------------------------------------



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Transform Coordinates from Sketch to Model Space Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:




x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2015 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.