Python SolidWorks secondary development—SolidWorks four ways to traverse components (3)
Article directory
Preface
The traversal of components is mainly to facilitate subsequent operations on components, such as property modification of batch components, equation modification, etc. Here are four traversal methods that come with SolidWorks. This article introduces the third traversal method. The first two types of traversal The link method is as follows:
Python SolidWorks secondary development—SolidWorks four ways to traverse components (1)
Python SolidWorks secondary development—SolidWorks four ways to traverse components (2)
1. Official sample traversal code
In the SolidWorks official API help file, search for "Expand or Collapse FeatureManager Design Tree Nodes Example" to find specific sample code. The sample code contains many filter items. We are only traversing the components here, so we will modify the sample code. , the modified code is as follows. The principle of this traversal is to traverse Node by searching, and then find that the Node type is Component2 type to traverse:
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim myModel As SldWorks.ModelDoc2
Dim featureMgr As SldWorks.FeatureManager
Dim rootNode As SldWorks.TreeControlItem
Set swApp = Application.SldWorks
Set myModel = swApp.ActiveDoc
Set featureMgr = myModel.FeatureManager
Set rootNode = featureMgr.GetFeatureTreeRootItem()
If Not rootNode Is Nothing Then
traverse_node rootNode
End If
End Sub
Sub traverse_node(node As SldWorks.TreeControlItem)
Dim childNode As SldWorks.TreeControlItem
Dim componentNode As SldWorks.Component2
Dim nodeObject As Object
Dim refConfigName As String
Set nodeObject = node.Object
If Not nodeObject Is Nothing Then
Set componentNode = nodeObject
If componentNode.GetSuppression <> SwConst.swComponentSuppressionState_e.swComponentSuppressed Then
Debug.Print componentNode.Name2
End If
End If
Set childNode = node.GetFirstChild()
While Not childNode Is Nothing
If childNode.ObjectType = SwConst.swTreeControlItemType_e.swFeatureManagerItem_Component Then
traverse_node childNode
End If
Set childNode = childNode.GetNext
Wend
End Sub
2. Python traversal code
To facilitate subsequent comparison, the functions of the Python code and the functions of the VBA code remain the same and will not be deleted. The following is an example of the Python code:
import win32com.client
from swconst import constants
def traverse_node(node):
nodeObject = node.Object
if nodeObject is not None:
if nodeObject.GetSuppression!=constants.swComponentSuppressed:
print(nodeObject.Name2)
childNode = node.GetFirstChild
while childNode is not None:
if childNode.ObjectType==constants.swFeatureManagerItem_Component:
traverse_node(childNode)
childNode = childNode.GetNext
def main():
sldver=2018
swApp=win32com.client.Dispatch(f'SldWorks.Application.{
sldver-1992}')
swApp.CommandInProgress =True
swApp.Visible =True
myModel = swApp.ActiveDoc
featureMgr = myModel.FeatureManager
rootNode = featureMgr.GetFeatureTreeRootItem2(constants.swFeatMgrPaneBottom)
traverse_node(rootNode)
if __name__ == '__main__':
main()