Question to Brian

Question to Brian

Anonymous
Not applicable
491 Views
8 Replies
Message 1 of 9

Question to Brian

Anonymous
Not applicable
Is it possible to do the following thing
by code ?
Select a part in the Browser pane of an Drawing,
then mimic the select command, after that
start the command all edges and
move all the edges of the selected part to a new layer.

Kindest regards,

JesG
0 Likes
492 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable
I didn't do any error checking and only tested it on one case but this will
at least provide a starting point. I tried to add enough comments that
hopefully you can make some sense out of it.

Public Sub ChangeLayerOfPart()
Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument

' Call the function to find a specific node. The second argument of
this function
' is a string that would be within the full browser path of the node you
want to
' find. For example, if looking for an occurrence named Crank:1 that's
contained
' within the assembly 4Bar.iam, then you can use the search path
"4Bar.iam:Crank:1".
' The colon is used to seperate the individual components of the path.
(In this case
' one is also used in the name of the occurrence.)
Dim oNode As BrowserNode
Set oNode = GetBrowserNodeByName(oDoc.BrowserPanes.ActivePane.TopNode,
"4Bar.iam:Crank:1")

' Select the object represented by the node.
oNode.DoSelect

' Assuming the node represented a part within an assembly in a drawing
view, this
' then executes the "Select as Edges" command.
Dim oControlDef As ControlDefinition
Set oControlDef =
ThisApplication.CommandManager.ControlDefinitions.Item("SelectAsEdgesCtxCmd")
oControlDef.Execute

' Get the layer object that we want to use.
Dim oLayer As Layer
Set oLayer = oDoc.StylesManager.Layers.Item("Hidden")

' Iterate through the curves and change their layer.
Dim oDrawCurveSegment As DrawingCurveSegment
For Each oDrawCurveSegment In oDoc.SelectSet
oDrawCurveSegment.Layer = oLayer
Next
End Sub


Private Function GetBrowserNodeByName(ByVal TopNode As BrowserNode, ByVal
FullPathSearchString As String) As BrowserNode
Set GetBrowserNodeByName = Nothing

' Go through the child nodes of the node passed in.
Dim oSubNode As BrowserNode
For Each oSubNode In TopNode.BrowserNodes
' Check to see if this node is visible. Skip it if it is.
If oSubNode.Visible Then
' Check to see if the supplied search search is in the full path
of the current node.
If InStr(oSubNode.FullPath, FullPathSearchString) Then
' Set the return value and exit.
Set GetBrowserNodeByName = oSubNode
Exit Function
Else
' Check to see if this node has any children.
If oSubNode.BrowserNodes.Count > 0 Then
' Recursively call this function to traverse the tree.
Set GetBrowserNodeByName =
GetBrowserNodeByName(oSubNode, FullPathSearchString)
End If
End If
End If
Next
End Function
--
Brian Ekins
Autodesk Inventor API

wrote in message news:5205735@discussion.autodesk.com...
Is it possible to do the following thing
by code ?
Select a part in the Browser pane of an Drawing,
then mimic the select command, after that
start the command all edges and
move all the edges of the selected part to a new layer.

Kindest regards,

JesG
0 Likes
Message 3 of 9

Anonymous
Not applicable
Dear Brian,

thank you for the quick response.
is it possible to get the Model object from the Node?
I know that there is the method in the drawing document
ProcessViewSelection but i did not know how to cast the Node object
to the Generic object

Thanks in advance

JesG
0 Likes
Message 4 of 9

Anonymous
Not applicable
I thought about this some more and realized the browser is not a very good
approach for this. Instead you can access the assembly directly and find
the part(s) of interest in the assembly and then get their corresponding
edges in the drawing. Here's a new version of the program that does this.
In this case I'm using the name of the occurrence to find the match but you
could choose to use any method to find the part you want to change the layer
for.

Public Sub ChangePartLayer()
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument

On Error Resume Next
Dim oView As DrawingView
Set oView = oDrawDoc.SelectSet.Item(1)
If Err Then
MsgBox "A drawing view must be selected."
Exit Sub
End If

' Get the assembly displayed in the view.
Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
If Err Then
MsgBox "Unable to get the referenced assembly from the selected
view."
Exit Sub
End If
On Error GoTo 0

' Find the occurrence(s) of interest in the assembly. The name
specified can
' use wild cards and will match the occurrence name. More than once
match
' can occur and all matches will have the layer changed.
Dim oOccs As ObjectCollection
Call FindOccurrencesByName(oAsmDoc.ComponentDefinition.Occurrences,
"*Crank*", oOccs)

' Get the layer object that we want to use.
Dim oLayer As Layer
Set oLayer = oDrawDoc.StylesManager.Layers.Item("Hidden")

' Get the curves in the view associated with the found occurrences.
Dim oOcc As ComponentOccurrence
For Each oOcc In oOccs
Dim oDrawCurves As DrawingCurvesEnumerator
Set oDrawCurves = oView.DrawingCurves(oOcc)

' Iterate through the drawing curves associated with this part.
Dim oDrawCurve As DrawingCurve
For Each oDrawCurve In oDrawCurves
' Iterate through the segments in each curve and change their
layer.
Dim oDrawCurveSegment As DrawingCurveSegment
For Each oDrawCurveSegment In oDrawCurve.Segments
oDrawCurveSegment.Layer = oLayer
Next
Next
Next
End Sub


Private Sub FindOccurrencesByName(ByVal Occurrences As ComponentOccurrences,
ByVal SearchName As String, ByRef FoundOccurrences As ObjectCollection)
' Create the object collection, but only on the first entry into this
function.
Static Initialized As Boolean
If Not Initialized Then
Set FoundOccurrences =
ThisApplication.TransientObjects.CreateObjectCollection
Initialized = True
End If

Dim oOcc As ComponentOccurrence
For Each oOcc In Occurrences
' Check to see if this occurrence has a matching name.
If oOcc.name Like SearchName Then
' Add this occurrence to the collection.
FoundOccurrences.Add oOcc
End If

' Resursively call this function to traverse the entire assembly
tree.
Call FindOccurrencesByName(oOcc.SubOccurrences, SearchName,
FoundOccurrences)
Next
End Sub

--
Brian Ekins
Autodesk Inventor API


wrote in message news:5207009@discussion.autodesk.com...
Dear Brian,

thank you for the quick response.
is it possible to get the Model object from the Node?
I know that there is the method in the drawing document
ProcessViewSelection but i did not know how to cast the Node object
to the Generic object

Thanks in advance

JesG
0 Likes
Message 5 of 9

Anonymous
Not applicable
Is there anyway to modify this code to select features within a part? I am trying to assign features within a part drawing to specific layers. I am using Inventor 11. Message was edited by: mcleamic
0 Likes
Message 6 of 9

Anonymous
Not applicable
It turns out that it's fairly easy to support this for a feature. This is
because the DrawingCurves method also allows a feature as input. You just
have to be careful that you provide the feature in the correct way. In this
case since the drawing view is of an assembly, the provided feature needs to
be in the context of the assembly, which means we need to supply a proxy for
the feature. The code below does this.

Public Sub ChangePartLayer()
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument

On Error Resume Next
Dim oView As DrawingView
Set oView = oDrawDoc.SelectSet.Item(1)
If Err Then
MsgBox "A drawing view must be selected."
Exit Sub
End If

' Get the assembly displayed in the view.
Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
If Err Then
MsgBox "Unable to get the referenced assembly from the selected
view."
Exit Sub
End If
On Error GoTo 0

' Find the occurrence(s) of interest in the assembly. The name
specified can
' use wild cards and will match the occurrence name. More than once
match
' can occur and all matches will have the layer changed.
Dim oOccs As ObjectCollection
Call FindOccurrencesByName(oAsmDoc.ComponentDefinition.Occurrences,
"*Base*", oOccs)

' Get the layer object that we want to use.
Dim oLayer As Layer
Set oLayer = oDrawDoc.StylesManager.Layers.Item("Hidden (ANSI)")

' Get the curves in the view associated with the found occurrences.
Dim oOcc As ComponentOccurrence
For Each oOcc In oOccs
' Get the feature within the occurrence to change the layer for.
' This gets the feature named "Extrusion2".
Dim oPartDef As PartComponentDefinition
Set oPartDef = oOcc.Definition
Dim oNativeFeature As PartFeature
Set oNativeFeature = oPartDef.Features.Item("Extrusion2")

' Create a proxy for the feature.
Call oOcc.CreateGeometryProxy(oNativeFeature, oNativeFeature)

Dim oDrawCurves As DrawingCurvesEnumerator
Set oDrawCurves = oView.DrawingCurves(oNativeFeature)

' Iterate through the drawing curves associated with this part.
Dim oDrawCurve As DrawingCurve
For Each oDrawCurve In oDrawCurves
' Iterate through the segments in each curve and change their
layer.
Dim oDrawCurveSegment As DrawingCurveSegment
For Each oDrawCurveSegment In oDrawCurve.Segments
oDrawCurveSegment.Layer = oLayer
Next
Next
Next
End Sub


Private Sub FindOccurrencesByName(ByVal Occurrences As ComponentOccurrences,
ByVal SearchName As String, ByRef FoundOccurrences As ObjectCollection)
' Create the object collection, but only on the first entry into this
function.
Static Initialized As Boolean
If Not Initialized Then
Set FoundOccurrences =
ThisApplication.TransientObjects.CreateObjectCollection
Initialized = True
End If

Dim oOcc As ComponentOccurrence
For Each oOcc In Occurrences
' Check to see if this occurrence has a matching name.
If oOcc.Name Like SearchName Then
' Add this occurrence to the collection.
FoundOccurrences.Add oOcc
End If

' Resursively call this function to traverse the entire assembly
tree.
Call FindOccurrencesByName(oOcc.SubOccurrences, SearchName,
FoundOccurrences)
Next
End Sub

--
Brian Ekins
Autodesk Inventor API
0 Likes
Message 7 of 9

Anonymous
Not applicable
This works out, though I was hoping to get wildcards to work with the feature.item. Ultimately I would like to interrogate all of a part's feature's within a drawing, create new layers based on the feature name, and then move those features to the appropriate layers. I'm new to VBA and hope I'm not being to ambitious. Thank you very much for your help, it is greatly appreciated.
0 Likes
Message 8 of 9

Anonymous
Not applicable
It would be possible to support a wild card look-up for a feature. In the
example I sent you it's using the Item method to get a specific feature.
This doesn't support wild card matches but expects an exact match. Instead
you'll need to iterate through the entire collection and match the names
yourself. In this case you can do any type of matching that you want,
including wild cards. To do this you can use the Like operator. This is
demonstrated below.

Dim strMatchString As String
strMatchString = "Extrude*"

Dim i As Integer
For i = 1 To oPartFeatures.Count
If oPartFeatures.Item(i) Like strMatchString Then
Debug.Print "Match"
Else
Debug.Print "No match"
End If
Next
--
Brian Ekins
Autodesk Inventor API

wrote in message news:5517765@discussion.autodesk.com...
This works out, though I was hoping to get wildcards to work with the
feature.item. Ultimately I would like to interrogate all of a part's
feature's within a drawing, create new layers based on the feature name, and
then move those features to the appropriate layers. I'm new to VBA and hope
I'm not being to ambitious. Thank you very much for your help, it is
greatly appreciated.
0 Likes
Message 9 of 9

Anonymous
Not applicable
Thanks again Brian.
0 Likes