Hello together,
how could I find a part with a given name in the browser of a drawing?
Thanky you very much
Georg
He Georg,
Below is some VBA code that prints out names of parts in the browser. (most of this is from an example in the API Help). You could add an If statement to get the part you wanted. This may not do exactly what you want but it should show how to get to objects from the browser nodes.
Also there are other ways to get the parts and assemblies that are being used by a drawing. (The DrawingView.ReferencedDocumentDescriptor for example)
Private Sub QueryModelTree()
Dim Doc As Document
If (ThisApplication.Documents.count <> 0) Then
Set Doc = ThisApplication.ActiveDocument
Else
MsgBox "There are no open documents!"
Exit Sub
End If
Dim oTopNode As BrowserNode
Set oTopNode = Doc.BrowserPanes("Model").TopNode
Call recurse(oTopNode)
End Sub
Sub recurse(node As BrowserNode)
If (node.Visible = True) Then
' Debug.Print node.BrowserNodeDefinition.Label
Dim oNativeObj As Object
Set oNativeObj = node.NativeObject
' Debug.Print TypeName(oNativeObj)
Dim oDoc As Document
Dim oAsmCompDef As AssemblyComponentDefinition
Dim oCompOcc As ComponentOccurrence
Dim oCompOccproxy As ComponentOccurrenceProxy
If TypeName(oNativeObj) = "AssemblyComponentDefinition" Then
Set oAsmCompDef = node.NativeObject
Set oDoc = oAsmCompDef.Document
Debug.Print oDoc.DisplayName
End If
If TypeName(oNativeObj) = "ComponentOccurrence" Then
Set oCompOcc = node.NativeObject
Set oDoc = oCompOcc.Definition.Document
Debug.Print oDoc.DisplayName
End If
If TypeName(oNativeObj) = "ComponentOccurrenceProxy" Then
Set oCompOccproxy = node.NativeObject
Debug.Print oCompOccproxy.name
Set oDoc = oCompOccproxy.NativeObject.Definition.Document
Debug.Print oDoc.DisplayName
End If
Dim bn As BrowserNode
For Each bn In node.BrowserNodes
Call recurse(bn)
Next
End If
End Sub
Thanks,
Wayne
Hello Wayne,
thanks for the code. But thats not what I am looking for.
Georg
Hi Georg,
Please add more detail to what you are trying to do. It may help to attach a drawing and the parts it is using.
Thanks,
Wayne
Hello Wayne,
how could I select the part in the browser and expand it? I know the name from the part already.
Thank you
Georg
Hi,
How is the Part selected? Are you asking the user to select the part? I am asking this because from what I can determine a drawing curve needs to be selected in the view to be able to get the document. (to get the parts name). If your code is prompting the user to select a part in a view you could use a filter so the user can only select a drawing curve. (kDrawingCurveSegmentFilter) See this example in the help on how to create a select event in VBA. (Sub TestSelection)
When a Part is selected in a drawing view the selected object in the SelectSet is a GenericObject . (you can see this in the VBA Watch window) The GenericObject does not provide any properties to get information from the API.
Here is a VBA example that is just showing what part of the API I am suggesting to get to the document. If an edge (not an edge of a face) of a part in a view is selected a MsgBox will display the document name and view name. (this example could be updated to handle ModelGeometry returning a face)
Public Sub SelSetTest3()
Dim oDrwCrvSeg As Object
Set oDrwCrvSeg = ThisApplication.ActiveDocument.SelectSet(1)
If Not oDrwCrvSeg Is Nothing Then
Dim oDrwCrv As DrawingCurve
Set oDrwCrv = oDrwCrvSeg.Parent
Dim oEdge As Edge
Set oEdge = oDrwCrv.ModelGeometry
Dim oSrfBody As SurfaceBody
Set oSrfBody = oEdge.Parent
Dim oCompDef As ComponentDefinition
Set oCompDef = oSrfBody.ComponentDefinition
Dim oDoc As Document
Set oDoc = oCompDef.Document
MsgBox oDoc.FullFileName
Dim oDrwView As DrawingView
Set oDrwView = oDrwCrv.Parent
MsgBox oDrwView.Name
End If
End Sub
Thanks,
Wayne
Hello Wayne,
thank you very much for your help. But how could I bring the code together? I could not get the part selected in the browser.
Georg
Hi Everyone,
This DevBlog post has a VB.NET example
Thanks,
Wayne