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. Using the IMathUtility object, it is possible to display the sketch point’s coordinates in relation to the model origin.  

  1. Obtain the IMathUtility object from the current model document.

  2. Select a sketch that contains the points whose coordinates are to be modified.  

  3. Call ISketch::GetSketchPoints to fill a VARIANT array with all of the points in the sketch.  

  4. Build a coordinate array by filling the cells of a length 3 array with the x, y, and z value of a sketch point from the VARIANT array.  

  5. Create a new IMathPoint from the IMathUtility object, providing the coordinate array for the location of the IMathPoint.  

  6. Get the model-to-sketch transform for this sketch. Calling IMathTransform::Inverse provides a IMathTransform from the sketch coordinates to the model coordinates. The IMathPoint is then moved into the model coordinates by calling IMathPoint::MulitplyTransform(MathTransform).

Implementing this in code is fairly straightforward:

 

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

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 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 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) 2012 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.