AnsweredAssumed Answered

Skip pattern instances to skip component in looping?

Question asked by Prabaharan Pichaiyan on Jul 19, 2017
Latest reply on Jul 1, 2019 by 炜 刘

I got a macro to create a layers in assembly drawing by the name value of custom properties.

I'm facing some error while processing pattern components in assembly. If I have any skipped components in pattern, there its getting an error.

Could you please suggest some codes to skip those components?

 

Here are those codes,

 

Option Explicit
Sub main()
    Dim swApp       As SldWorks.SldWorks
    Dim swModel     As SldWorks.ModelDoc2
    Dim swDraw      As SldWorks.DrawingDoc
    Dim swSelMgr    As SldWorks.SelectionMgr
    Dim swView      As SldWorks.View
    Dim swDrawComp  As SldWorks.DrawingComponent
      
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDraw = swModel
    Set swSelMgr = swModel.SelectionManager
    Set swView = swSelMgr.GetSelectedObject6(1, -1)
    Set swDrawComp = swView.RootDrawingComponent
    ProcessDrawingComponent swApp, swDraw, swDrawComp, ""
End Sub
  
Sub ProcessDrawingComponent(swApp As SldWorks.SldWorks, swDraw As SldWorks.DrawingDoc, swDrawComp As SldWorks.DrawingComponent, sPadStr As String)
    Dim vDrawCompChildArr   As Variant
    Dim vDrawCompChild      As Variant
    Dim swDrawCompChild     As SldWorks.DrawingComponent
    
    Dim swComp As SldWorks.Component2
    Dim swCompModel As SldWorks.ModelDoc2
    Dim swCustProp As CustomPropertyManager
    Dim val As String
    Dim valout As String
    Dim chkresult As Integer
  
Set swComp = swDrawComp.Component
Set swCompModel = swComp.GetModelDoc2


If swComp.GetSuppression <> 0 Then
Debug.Print "      Component                            = " & swComp.Name2
If swCompModel.GetType = 1 Then


Set swCustProp = swCompModel.Extension.CustomPropertyManager("")
swCustProp.Get4 "Description", False, val, valout 'Change property name here
If InStr(valout, "Test") = 1 Then


ChangeComponentLayer swApp, swDraw, swDrawComp, UCase(valout),
    vDrawCompChildArr = swDrawComp.GetChildren
    If Not IsEmpty(vDrawCompChildArr) Then
        For Each vDrawCompChild In vDrawCompChildArr
            Set swDrawCompChild = vDrawCompChild
                 ProcessDrawingComponent swApp, swDraw, swDrawCompChild, sPadStr + "  "
        Next
    End If
 End Sub
  
Private Sub ChangeComponentLayer(swApp As SldWorks.SldWorks, swDraw As SldWorks.DrawingDoc, swDrawComp As SldWorks.DrawingComponent, sLayerName As String)
    Dim bRet As Boolean
    bRet = swDraw.CreateLayer(sLayerName, sLayerName, 0, swLineCONTINUOUS, swLW_NORMAL, True): Debug.Assert bRet
    swDrawComp.Layer = sLayerName
End Sub

 

 

Thank you

Outcomes