Hide Table of Contents

Transform Sketch to Model Example (VBA)

When a sketch point is created, its x, y, and z location values are in relation to the sketch origin. This example shows how to get the sketch point’s coordinates in relation to the model origin using a MathUtility object.  

'-----------------------------------------------------------------------
' Preconditions:
'  1. Open a part.
'  2. Select a sketch.
'
' Postconditions:
'  1. Gets a MathUtility object from the current model document.
'  2. Gets the selected sketch that contains the points whose
'     coordinates to modify.
'  3. Fills n array with all of the points in the sketch.
'  4. Creates a coordinate array with the x, y, and z value sof a sketch point
'     from the sketch-point array.
'  5. Creates a new MathPoint object from the MathUtility object, providing
'     the coordinate array for the location of the MathPoint.
'  6. Gets and displays the model-to-sketch transform for this sketch.
'  7. Click OK to close the message box.
'  8. Calls IMathTransform::Inverse, which provides a MathTransform
'     object from the sketch coordinates to the model coordinates.
'  9. Calls IMathPoint::MulitplyTransform(MathTransform) to move
'     the MathPoint object into the model.
' 10. Displays the point coordinates in relation to the model origin.
' 11. Click OK to close the message box.
'--------------------------------------------------------------------------
Option Explicit
    Dim swApp As SldWorks.SldWorks
    Dim selMgr As SldWorks.SelectionMgr
    Dim Model As SldWorks.ModelDoc2
    Dim SketchPoints As Variant
    Dim SketchFeature As SldWorks.Feature
    Dim PointCoords(2) As Double
    Dim MathUtil As SldWorks.MathUtility
    Dim MathTrans As SldWorks.MathTransform
    Dim MathP As SldWorks.MathPoint
    Dim ModelSketchTransform As Variant
Sub main()
    'Connect the program to SOLIDWORKS
    Set swApp = CreateObject("SldWorks.Application")
    Set Model = swApp.ActiveDoc
    'Prepare the MathUtility
    Set MathUtil = swApp.GetMathUtility
    'Get the SelectionMgr
    Set selMgr = Model.SelectionManager
    'Get the sketch from the SelectionMgr
    Set SketchFeature = selMgr.GetSelectedObject6(1, 0)
    Set SketchFeature = SketchFeature.GetSpecificFeature2
    'Get the sketch points
    SketchPoints = SketchFeature.GetSketchPoints2
    'Build a coordinate array from the first point in the sketch
    PointCoords(0) = SketchPoints(0).X
    PointCoords(1) = SketchPoints(0).Y
    PointCoords(2) = SketchPoints(0).Z
    'Create the new MathPoint from the sketch point data
    'MathP refers to the point location in the sketch coordinates
    Set MathP = MathUtil.CreatePoint(PointCoords)
    'Display the point coordinates in relation to the sketch origin
    SketchPoints = MathP.ArrayData
    MsgBox "Point coordinates in relation to the sketch origin: " & SketchPoints(0) & ", " & SketchPoints(1) & ", " & SketchPoints(2)
    'Get the model-to-sketch transform for this sketch
    Set MathTrans = SketchFeature.ModelToSketchTransform
    'Get the inversion of the transform
    Set MathTrans = MathTrans.Inverse
    'Multiply the point by the inverse transform
    'MathP now refers to the point location in the model coordinates
    Set MathP = MathP.MultiplyTransform(MathTrans)
    'Display the point coordinates in relation to the model origin
    SketchPoints = MathP.ArrayData
    MsgBox "Point coordinates in relation to the model origin: " & SketchPoints(0) & ", " & SketchPoints(1) & ", " & SketchPoints(2)
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 Sketch to Model 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) 2019 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.