Search code examples
vbasolidworks

How to get coordinate data of objects in SolidWorks using API


I want to know how to access coordinates of different objects in the SolidWorks feature tree via the API which uses VBA. The problem for me personally is to find the correct statement to extract the positional data which varies depending on the selected object. My objects are described through their own coordinate system and an origin point.

Thanks in advance


Solution

  • For components you can use the .Transform2 property. This will return a MathTransform object which contains the transofmration matrix data for the component. You can access the data of the MathTransform object with the .ArrayData property. This will return a array of 16 doubles. The first 9 elements define the 3x3 rotation matrix and the next 3 elements define the translation component (the xyz origin point of the component). In the API help you can find detailed informations.

    In the API help is an example for how to get the transforms of assembly components: http://help.solidworks.com/2019/English/api/sldworksapi/Get_Transforms_of_Assembly_Components_Example_VB.htm

    My objects are described through their own coordinate system and an origin point

    If you are using the reference geometry "coordinate system" as custom origin, you can access the MathTransform via .Transform of its CoordinateSystemFeatureData object.

    To get the CoordinateSystemFeatureData object you have to first get the Feature object of your coordinate system - then call .GetDefinition

    Example:

    Dim swApp As SldWorks.SldWorks
    Dim Part As SldWorks.ModelDoc2
    Dim boolstatus As Boolean
    Dim longstatus As Long, longwarnings As Long
    
    Sub main()
    
    Set swApp = Application.SldWorks
    
    Set Part = swApp.ActiveDoc
    Part.ClearSelection2 True
    boolstatus = Part.Extension.SelectByID2("FEATURE NAME OF COORDINATE SYSTEM", "COORDSYS", 0, 0, 0, False, 0, Nothing, 0)
    
    Dim swSelMgr As SelectionMgr
    Set swSelMgr = Part.SelectionManager
    
    Dim swFeat As Feature
    Set swFeat = swSelMgr.GetSelectedObject6(1, -1)
    
    Dim swFeatData As CoordinateSystemFeatureData
    Set swFeatData = swFeat.GetDefinition
    
    End Sub