How to use DrawingGetModelSketchesCtxCmd

How to use DrawingGetModelSketchesCtxCmd

brotherhogue
Contributor Contributor
284 Views
2 Replies
Message 1 of 3

How to use DrawingGetModelSketchesCtxCmd

brotherhogue
Contributor
Contributor

Goal: get model sketches for each drawing view.

I'm trying to use a combination of this...

 

DrawingView.ReferencedDocumentDescriptor Property
Parent Object: DrawingView
Description
Property that returns the model document referenced by this view.

Syntax
DrawingView.ReferencedDocumentDescriptor() As DocumentDescriptor
Property Value
This is a read only property whose value is a DocumentDescriptor.

 

...Combined with this...

BrowserPane.GetBrowserNodeFromObject Method
Parent Object: BrowserPane
Description
Returns the browser node that corresponds to the input object. The method returns an error if no corresponding node is found within this pane. If multiple matches are found, the method returns the first match.

Syntax
BrowserPane.GetBrowserNodeFromObject( NativeObject As Object ) As BrowserNode

...and adding each node for each view to a selection set before running the command:

DrawingGetModelSketchesCtxCmd

I've so far been unsuccessful and getting desperate. I also tried this...

DrawingView.SetIncludeStatus Method
Parent Object: DrawingView
Description
Method that sets the include status of the input object in the drawing view. This method automatically makes the object visible as well. After an object has been included, its visibility can be controlled using the GetVisibility and SetVisibility methods.

Syntax
DrawingView.SetIncludeStatus( Object As Object, Include As Boolean )
Parameters
Name Type Description
Object Object Input object to set the include status of. Valid objects are 2d and 3d sketches, work features, surface features, and proxies for all of these. The object needs to be supplied in the context of the document referenced by the drawing view. For instance, to set the include status of a 3D sketch within a part instanced in an assembly (and the drawing view is of the assembly), the input should be a Sketch3DProxy object created in context of the assembly. An error will occur if no corresponding object exists in the drawing view.
Include Boolean Input Boolean that specifies whether the input object is included in the drawing view.

But I was also unsuccessful this way. Can anybody tell me what I might be doing wrong, and suggest a snippet of code that would help me target the browser nodes for all models from all drawing views on the active sheet of the active document and get the model sketches?

Thank you so much.

0 Likes
285 Views
2 Replies
Replies (2)
Message 2 of 3

C_Haines_ENG
Collaborator
Collaborator

What is the general purpose of your code, step by step? I feel like you may be overcomplicating the method. 

0 Likes
Message 3 of 3

brotherhogue
Contributor
Contributor

The goal is to get sketches I've made in the flat pattern definition of a sheet metal parts to be visible in all drawing views on the active sheet of the active document. I have no preference on how to do that, I just thought the objects above would be useful and I haven't quite figured out how to do it in an optimal way. this code currently works for me though:

AddReference "System.Windows.Forms"
AddReference "System.Drawing"

Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Drawing
Imports Inventor
Imports System

Public Class BrowserNodeTest
    Private inventorApp As Inventor.Application
    Private DrawingDoc As Inventor.DrawingDocument
    Private _commandDefinition As Inventor.ControlDefinition
    Private _loggerDelegate As Action(Of String)
    Private ViewNode As BrowserNode
    Private ModelNode As BrowserNode

    Public Sub New(app As Inventor.Application, loggerDelegate As Action(Of String))
        inventorApp = app
        DrawingDoc = TryCast(inventorApp.ActiveDocument, Inventor.DrawingDocument)
        _loggerDelegate = loggerDelegate
    End Sub

    Private Sub LogInfo(message As String)
        If _loggerDelegate IsNot Nothing Then
            _loggerDelegate("INFO: " & message)
        End If
    End Sub

    Private Sub LogError(message As String)
        If _loggerDelegate IsNot Nothing Then
            _loggerDelegate("ERROR: " & message)
        End If
    End Sub

    Public Sub ProcessView(view As Inventor.DrawingView)
        LogInfo("Processing view: " & view.Name)
        
        Dim browserPane As Inventor.BrowserPane = Nothing
        For Each pane As Inventor.BrowserPane In DrawingDoc.BrowserPanes
            LogInfo("Found browser pane: " & pane.Name)
            If pane.Name = "Model" Then
                browserPane = pane
                Exit For
            End If
        Next
        
        If browserPane Is Nothing Then
            LogError("Model browser pane not found")
            Return
        End If

        inventorApp.CommandManager.ControlDefinitions.Item("AppBrowserExpandAllCmd").Execute()
        LogInfo("Expanded all browser nodes")

        Dim validPairs As New List(Of Tuple(Of BrowserNode, BrowserNode))
        
        For Each sheetNode As Inventor.BrowserNode In browserPane.TopNode.BrowserNodes
            If sheetNode.BrowserNodeDefinition.Label = "Drawing Resources" Then Continue For
            
            If TypeName(sheetNode.BrowserNodeDefinition.NativeObject) = "Sheet" Then
                LogInfo("Found sheet node: " & sheetNode.BrowserNodeDefinition.Label)
                
                For Each viewNode As Inventor.BrowserNode In sheetNode.BrowserNodes
                    LogInfo("Checking view node: " & viewNode.BrowserNodeDefinition.Label)
                    
                    For Each modelNode As Inventor.BrowserNode In viewNode.BrowserNodes
                        If TypeName(modelNode.BrowserNodeDefinition.NativeObject) = "SheetMetalComponentDefinition" Then
                            LogInfo("Found valid pair - View: " & viewNode.BrowserNodeDefinition.Label & ", Model: " & modelNode.BrowserNodeDefinition.Label)
                            validPairs.Add(New Tuple(Of BrowserNode, BrowserNode)(viewNode, modelNode))
                        End If
                    Next
                Next
            End If
        Next

        LogInfo("Processing " & validPairs.Count & " valid node pairs")
        
        For Each pair As Tuple(Of BrowserNode, BrowserNode) In validPairs
            DrawingDoc.SelectSet.Clear()
            LogInfo("Processing pair - View: " & pair.Item1.BrowserNodeDefinition.Label)
            
            Try
                pair.Item1.DoSelect()
                LogInfo("View node selected")
                pair.Item2.DoSelect()
                LogInfo("Model node selected")
                
                LogInfo("Selection count: " & DrawingDoc.SelectSet.Count)
                
                _commandDefinition = inventorApp.CommandManager.ControlDefinitions.Item("DrawingGetModelSketchesCtxCmd")
                _commandDefinition.Execute()
                LogInfo("Command executed for current pair")
                
            Catch ex As Exception
                LogError("Failed processing pair: " & ex.Message)
            End Try
        Next

        inventorApp.CommandManager.ControlDefinitions.Item("AppBrowserCollapseAllCmd").Execute()
        LogInfo("Collapsed all browser nodes")
        
        DrawingDoc.SelectSet.Clear()
        LogInfo("Cleared final selection state")
    End Sub
End Class

Public Sub Main()
    If ThisApplication.ActiveDocument.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
        MessageBox.Show("Please activate a drawing document before using this tool.",
                      "Invalid Document Type",
                      MessageBoxButtons.OK,
                      MessageBoxIcon.Warning)
        Return
    End If
    
    If String.IsNullOrEmpty(ThisApplication.ActiveDocument.FullFileName) Then
        MessageBox.Show("Please save the document before running this rule.",
                      "Drawing Export",
                      MessageBoxButtons.OK,
                      MessageBoxIcon.Information)
        Return
    End If
    
    Dim test As New BrowserNodeTest(ThisApplication, AddressOf LogMessage)
    test.ProcessView(ThisApplication.ActiveDocument.ActiveSheet.DrawingViews.Item(1))
End Sub

Public Sub LogMessage(message As String)
    If message.StartsWith("INFO:") Then
        Logger.Info(message.Substring(5).Trim())
    ElseIf message.StartsWith("ERROR:") Then
        Logger.Error(message.Substring(6).Trim())
    Else
        Logger.Info(message)
    End If
End Sub
0 Likes