Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IRstuff on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

API, Assembly traversal, grab children attribute 4

Status
Not open for further replies.

ShaggyPE

Mechanical
Sep 8, 2003
1,127
I am trying to do a seemingly simple task that is to be a portion of a much larger macro. I have created several test components and assemblies. The children of the top assy have all been assigned a custom property called "Level". The value associated with "Level" is 0 or 1. The function of this macro is to traverse through the children of the already open top assy, looking at the Level attribute. It will then create and assign a Level attribute to the top assy. The value of this attribute will be one higher than the highest of the children components.

The area I am having trouble with is grabbing the value of the attribute from the children. I downloaded a sample macro called TraverseAssembly to help me creating this one. The sample worked well to output the name of the children, I want to dive just a bit deeper. Below is my code. In bold is the line of code where I receive an error. Any help on this would be appreciated. Mind you I don't have much of a background in code so specific info is appreciated.
-Shaggy


Const swDocASSEMBLY = 2

Dim swApp As SldWorks.SldWorks
Dim ModelDoc As SldWorks.ModelDoc2
Dim Level As Integer
Dim strText As Variant

Sub Main()

Dim swRootComp As SldWorks.Component2
Dim swConf As SldWorks.configuration
Dim FileName As String

Set swApp = Application.SldWorks
Set ModelDoc = swApp.ActiveDoc


If Not ModelDoc Is Nothing Then
If ModelDoc.GetType = swDocASSEMBLY Then
'Get the active configuration
Set swConf = ModelDoc.GetActiveConfiguration
Set swRootComp = swConf.GetRootComponent
'Traverse the assembly
TraverseComponent swRootComp
End If
End If
ModelDoc.AddCustomInfo3 "", Level, swCustomInfoText, ""
ModelLevel = childlevel + 1
ModelDoc.CustomInfo2("", Level) = ModelLevel

End Sub


Sub TraverseComponent(swComp As SldWorks.Component2)
'this recursively traverses all of the components in an assembly
Dim vChildComp As Variant
Dim swChildComp As SldWorks.Component2
Dim i As Long
Dim childlevel As Long
Dim childlevel_temp As Long
Dim ModDoc As Object ' ModelDoc2 of child component
Dim ModName As String


vChildComp = swComp.GetChildren
childlevel_temp = 0
childlevel = 0
For i = 0 To UBound(vChildComp)
Set swChildComp = vChildComp(i)
ModName = swChildComp.Name
Set ModDoc = swChildComp.GetModelDoc()
childlevel_temp = swChildComp.GetCustomInfoValue("", Level)
If childlevel_temp > childlevel Then
childlevel = childlevel_temp
End If
Next i
End Sub
 
Replies continue below

Recommended for you

Fixed the "Part" problem. Assembly has to be loaded in memory (not lightweight) for the components to be "seen".
The question now is: In TraverseComponent I want to pass control to a form and stop execution until form is updated and processed. Then return to TraverseComponent for next component. What happens now is the form is loaded and shown then without waiting or input, the execution returns to TraverseComponent and continues the loop until the end.
How do I keep the control to the form, until I hit an "Ok" button or so.
 
frmCustomProperties.Show
frmCustomProperties.txtTitle.SetFocus
While frmCustomProperties.Visible
DoEvents
Wend

Of course, the final line of the "OK" button's OnClick event would be Me.Hide, which hides the form, turning its "Visible" property to "False".

Alternately, in the properties of the form itself you can specify its Modality. I believe you can make it either Application Modal, which freezes all SW functionality while it's visible, or System Modal, which freezes your system until it's hidden.
 
handleman, thank you for being out there.

From MSDN Library:

If a form is displayed as modal, the code following the Show method is not executed until the dialog box is closed. However, when a form is shown as modeless, the code following the Show method is executed immediately after the form is displayed.

So you are right and you deserve another star.

 
This is really weird.

Code:
    bExclude = swChildComp.ExcludeFromBOM 'evaluates to True
    If (Not bExclude) Then Call StartForm

My expectation was that when the boolean bExclude returns True, the Call StartForm will not be executed. But it is!!!

If I modify the code like this to explicitly set bExclude value,

Code:
    bExclude = swChildComp.ExcludeFromBOM 'evaluates to True
    [COLOR=red]bExclude = True[/color]
    If (Not bExclude) Then Call StartForm

the Call StartForm is skipped, as expected

 
After closing and opening VB and changing the declaration of bExclude from "Dim bExclude As Boolean" to "Dim bExclude" everything is fine.

Obviously there's another hurdle. My "Part" in the code above is a ModelDoc2 type (or so I think). So
ModelDoc2.DeleteCustomInfo2..
ModelDoc2.AddCustomInfo3..
ModelDoc2.CustomInfo2...

should work. The last one does, the first one doesn't. I don't get an error, it's just doesn't delete the properties. It's not a "case" issue.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor