×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

SolidWorks API, how can I know wich parameters are equation driven?

SolidWorks API, how can I know wich parameters are equation driven?

SolidWorks API, how can I know wich parameters are equation driven?

(OP)
Hi, I've write (taking code from here and there in Internet) a script to extract all dimensions from some drawings so I can work with them with Excel. The thing is some of these are formula driven in the drawing so I don't want to extract those. Any ideas?

See the code I wrote.

Regards,
Manolo

'Dimensions in m and rad
'In d:\tmp only PART and ASSEMBLY files
Sub main()
    'Variables SW
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swFeat As SldWorks.Feature
    Dim swSubFeat As SldWorks.Feature
    Dim swDispDim As SldWorks.DisplayDimension
    Dim swDim As SldWorks.Dimension
    Dim swAnn  As SldWorks.Annotation
    Dim bRet As Boolean
    Dim sCadena As String
    Dim sFicheroTemp As String
    Dim sFichero As String
    
    'Variables for files
    Dim fso As FileSystemObject
    Dim objFile As File
    Dim objFolder As Folder
    Dim objFileCol As Variant
    Dim FolderName As String
    Dim FileName As String
    Dim objRegEx As RegExp
    Dim lErrors As Long
    Dim lWarnings As Long
    
    Set swApp = Application.SldWorks
    Set fso = New FileSystemObject
    Set objRegEx = New RegExp
    objRegEx.IgnoreCase = True
    objRegEx.Pattern = "\.SLDPRT"
    
    FolderName = "D:\tmp\"
    Set objFolder = fso.GetFolder(FolderName)
    Set objFileCol = objFolder.Files
    sFicheroTemp = FolderName & "temp.txt"
    For Each objFile In objFileCol
        FileName = FolderName + objFile.Name
        If objRegEx.Test(objFile.Name) Then
           Set swModel = swApp.OpenDoc6(FileName, swDocPART, swOpenDocOptions_ReadOnly, "", lErrors, lWarnings)
        Else
           Set swModel = swApp.OpenDoc6(FileName, swDocASSEMBLY, swOpenDocOptions_ReadOnly, "", lErrors, lWarnings)
        End If
        
        Set swFeat = swModel.FirstFeature
        Do While Not swFeat Is Nothing
            Set swDispDim = swFeat.GetFirstDisplayDimension
            Do While Not swDispDim Is Nothing
                Set swAnn = swDispDim.GetAnnotation
                Set swDim = swDispDim.GetDimension
                sCadena = swDim.FullName & "*" & swDim.GetSystemValue2("")
                Open sFicheroTemp For Append Access Write As #1
                Print #1, sCadena
                Close #1
                'Sigue con el Loop
                Set swDispDim = swFeat.GetNextDisplayDimension(swDispDim)
            Loop
        Set swFeat = swFeat.GetNextFeature
        Loop
        bRet = swApp.CloseAllDocuments(False)
    Next
    
    'Eliminar las líneas repetidas

    Const adOpenStatic = 3
    Const adLockOptimistic = 3
    Const adCmdText = &H1
    Dim objConnection As Object
    Dim objRecordset As Object
    Dim strPathtoTextFile As String
    Dim strFile As String
    
    
    Set objConnection = CreateObject("ADODB.Connection")
    Set objRecordset = CreateObject("ADODB.Recordset")

    strPathtoTextFile = "D:\tmp\"
    strFile = "temp.txt"
    sFichero = FolderName & "variables.txt"

    objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source=" & strPathtoTextFile & ";" & _
          "Extended Properties=""text;HDR=NO;FMT=Delimited"""

    objRecordset.Open "Select DISTINCT * FROM " & strFile, _
        objConnection, adOpenStatic, adLockOptimistic, adCmdText

    Open sFichero For Output Access Write As #1
    Do Until objRecordset.EOF
        sCadena = objRecordset.Fields.Item(0).Value
        sCadena = Replace(sCadena, "*", ";")
        Print #1, sCadena
        'Wscript.Echo objRecordset.Fields.Item(0).Value
        objRecordset.MoveNext
    Loop
    Close #1
End Sub

RE: SolidWorks API, how can I know wich parameters are equation driven?

You should use the IDimension::DrivenState property. If the returned value is swDimensionDriven then the dimension driven by equation.

Please take a look at the following macro. If the selected dimension is driven the output value is True and False in another case:

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swDispDim As SldWorks.DisplayDimension
Dim swDim As SldWorks.Dimension

Sub main()

    Set swApp = Application.SldWorks
    
    Set swModel = swApp.ActiveDoc
    
    Set swSelMgr = swModel.SelectionManager
    
    Set swDispDim = swSelMgr.GetSelectedObject6(1, -1)
    
    Set swDim = swDispDim.GetDimension2(1)
    
    Debug.Print swDim.DrivenState = swDimensionDrivenState_e.swDimensionDriven
    
End Sub
 

Artem Taturevich
CSWP

RE: SolidWorks API, how can I know wich parameters are equation driven?

(OP)
Thanks very much. Here is the complete macro to extract al dimensions that are not equation driven from all PART or ASM files in D:\tmp (there can't be any other type of files).

Sub main()
    'Variables SW
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swFeat As SldWorks.Feature
    Dim swSubFeat As SldWorks.Feature
    Dim swDispDim As SldWorks.DisplayDimension
    Dim swDim As SldWorks.Dimension
    Dim swAnn  As SldWorks.Annotation
    Dim bRet As Boolean
    Dim sCadena As String
    Dim sFicheroTemp As String
    Dim sFichero As String
    
    'Variables para ficheros
    Dim fso As FileSystemObject
    Dim objFile As File
    Dim objFolder As Folder
    Dim objFileCol As Variant
    Dim FolderName As String
    Dim FileName As String
    Dim objRegEx As RegExp
    Dim lErrors As Long
    Dim lWarnings As Long
    
    Set swApp = Application.SldWorks
    Set fso = New FileSystemObject
    Set objRegEx = New RegExp
    objRegEx.IgnoreCase = True
    objRegEx.Pattern = "\.SLDPRT"
    
    FolderName = "D:\tmp\"
    Set objFolder = fso.GetFolder(FolderName)
    Set objFileCol = objFolder.Files
    sFicheroTemp = FolderName & "temp.txt"
    For Each objFile In objFileCol 'Para cada archivo en la carpeta
        FileName = FolderName + objFile.Name
        If objRegEx.Test(objFile.Name) Then
           Set swModel = swApp.OpenDoc6(FileName, swDocPART, swOpenDocOptions_ReadOnly, "", lErrors, lWarnings)
        Else
           Set swModel = swApp.OpenDoc6(FileName, swDocASSEMBLY, swOpenDocOptions_ReadOnly, "", lErrors, lWarnings)
        End If
        
        'Busca todas las dimensiones del archivo
        Set swFeat = swModel.FirstFeature
        Do While Not swFeat Is Nothing
            Set swDispDim = swFeat.GetFirstDisplayDimension
            Do While Not swDispDim Is Nothing
                Set swAnn = swDispDim.GetAnnotation
                Set swDim = swDispDim.GetDimension
                
                If swDim.DrivenState <> swDimensionDrivenState_e.swDimensionDriven Then
                    sCadena = swDim.FullName & "*" & swDim.GetSystemValue2("")
                    'Añade las variables al fichero de texto
                    Open sFicheroTemp For Append Access Write As #1
                    Print #1, sCadena
                    Close #1
                    'Sigue con el Loop
                End If
                Set swDispDim = swFeat.GetNextDisplayDimension(swDispDim)
            Loop
        Set swFeat = swFeat.GetNextFeature
        Loop
        bRet = swApp.CloseAllDocuments(False)
    Next
    
    'Eliminar las líneas repetidas

    Const adOpenStatic = 3
    Const adLockOptimistic = 3
    Const adCmdText = &H1
    Dim objConnection As Object
    Dim objRecordset As Object
    Dim strPathtoTextFile As String
    Dim strFile As String
    
    
    Set objConnection = CreateObject("ADODB.Connection")
    Set objRecordset = CreateObject("ADODB.Recordset")

    strPathtoTextFile = "D:\tmp\"
    strFile = "temp.txt"
    sFichero = FolderName & "variables.txt"

    objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source=" & strPathtoTextFile & ";" & _
          "Extended Properties=""text;HDR=NO;FMT=Delimited"""

    objRecordset.Open "Select DISTINCT * FROM " & strFile, _
        objConnection, adOpenStatic, adLockOptimistic, adCmdText

    Open sFichero For Output Access Write As #1
    Do Until objRecordset.EOF
        sCadena = objRecordset.Fields.Item(0).Value
        sCadena = Replace(sCadena, "*", ";")
        Print #1, sCadena
        'Wscript.Echo objRecordset.Fields.Item(0).Value
        objRecordset.MoveNext
    Loop
    Close #1
End Sub

RE: SolidWorks API, how can I know wich parameters are equation driven?

Dimension::DrivenState does not definitively determine if a parameter is driven by an equation.

RE: SolidWorks API, how can I know wich parameters are equation driven?

The most certain way to determine which parameters are equation driven is to access the Equation Mnaager.  From there, go through each of the equations and parse out which parameters are being driven.

batHonesty may be the best policy, but insanity is a better defense.bat
http://www.EsoxRepublic.com-SolidWorks API VB programming help

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources