Hello, i have a question.
in an assembly with many Part-files, where some parts have 3d-sketches inside, i am looking for a way to access (and set visibility) of these 3d-Sketches.
i can access the sketches, if i only have one part file open. but not with whole assembly.
Is there a list of all 3d-Sketches of the model or something like that?
thank you in advance
SRoos
Solved! Go to Solution.
Solved by WCrihfield. Go to Solution.
Try this code. It searches through all the browser nodes until it finds a 3D Sketch and turns it off. This should work for setting visibility of other types of objects as well.
To set visibility, change the second parameter of the first line that starts with "Call"
Public Sub Set3DSketchVisibility()
Dim thisAssembly As Document
Set thisAssembly = ThisApplication.ActiveDocument
If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then
MsgBox ("This macro can only be run in assembly documents")
Exit Sub
End If
'get the model browser pane
Dim oModelBrowserPane As BrowserPane
Set oModelBrowserPane = thisAssembly.BrowserPanes.item("Model")
'search through all browser nodes and set visibility of sketches found
'set the second parameter to true or false based on whether you want to hide or show 3d sketches
Call SearchNodes(oModelBrowserPane.TopNode.BrowserNodes, True)
End Sub
Private Sub SearchNodes(oNode As BrowserNodesEnumerator, sketchVisiblilty As Boolean)
'set instance objects
Dim oSubNode As BrowserNode
Dim o3DSketch As Sketch3DProxy
'quits routine when it comes to a node that is empty
If oNode.Count = 0 Then
Exit Sub
End If
'search through nodes
For Each oSubNode In oNode
'if node contains child nodes, search through those
If oSubNode.BrowserNodes.Count > 0 Then
Call SearchNodes(oSubNode.BrowserNodes, sketchVisiblilty)
End If
'search for 3D Sketches and set the visiblity
If Not oSubNode.NativeObject Is Nothing Then
If oSubNode.NativeObject.Type = kSketch3DProxyObject Then
Set o3DSketch = oSubNode.NativeObject
o3DSketch.Visible = sketchVisiblilty
End If
End If
Next
End Sub
You could also create a module and put both options as sub modules in there so you can call them from the ribbon.
Public Sub ShowAll3DSketches()
Dim thisAssembly As Document
Set thisAssembly = ThisApplication.ActiveDocument
If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then
MsgBox ("This macro can only be run in assembly documents")
Exit Sub
End If
'get the model browser pane
Dim oModelBrowserPane As BrowserPane
Set oModelBrowserPane = thisAssembly.BrowserPanes.item("Model")
'search through all browser nodes and set visibility of sketches found
Call SearchNodes(oModelBrowserPane.TopNode.BrowserNodes, True)
End Sub
Public Sub HideAll3DSketches()
Dim thisAssembly As Document
Set thisAssembly = ThisApplication.ActiveDocument
If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then
MsgBox ("This macro can only be run in assembly documents")
Exit Sub
End If
'get the model browser pane
Dim oModelBrowserPane As BrowserPane
Set oModelBrowserPane = thisAssembly.BrowserPanes.item("Model")
'search through all browser nodes and set visibility of sketches found
Call SearchNodes(oModelBrowserPane.TopNode.BrowserNodes, False)
End Sub
Private Sub SearchNodes(oNode As BrowserNodesEnumerator, sketchVisiblilty As Boolean)
'set instance objects
Dim oSubNode As BrowserNode
Dim o3DSketch As Sketch3DProxy
'quits routine when it comes to a node that is empty
If oNode.Count = 0 Then
Exit Sub
End If
'search through nodes
For Each oSubNode In oNode
'if node contains child nodes, search through those
If oSubNode.BrowserNodes.Count > 0 Then
Call SearchNodes(oSubNode.BrowserNodes, sketchVisiblilty)
End If
'search for 3D Sketches and set the visiblity
If Not oSubNode.NativeObject Is Nothing Then
If oSubNode.NativeObject.Type = kSketch3DProxyObject Then
Set o3DSketch = oSubNode.NativeObject
o3DSketch.Visible = sketchVisiblilty
End If
End If
Next
End Sub
Since you can access the API by several different means, I thought I would show the regular iLogic way to do it.
Here is a simple iLogic rule you can use to toggle the visibility of all 3D sketches in all parts within the active assembly.
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("This rule '" & iLogicVb.RuleName & "' only works for Assembly Documents.",vbOKOnly, "WRONG DOCUMENT TYPE")
Return
End If
Dim oADoc As AssemblyDocument = ThisAssembly.Document
Dim oOnOff As Boolean
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Dim oPDoc As PartDocument = oRefDoc
If oPDoc.ComponentDefinition.Sketches3D.Count > 0 Then
For Each oSketch3D As Sketch3D In oPDoc.ComponentDefinition.Sketches3D
oOnOff = oSketch3D.Visible
oSketch3D.Visible = (Not oOnOff)
Next
End If
End If
Next
If you want to change this rule from a toggle functionality to a direct (turn them all off) functionality, all you need to do is
Like this:
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("This rule only works for Assembly Documents.",vbOKOnly, "WRONG DOCUMENT TYPE")
Return ' or Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisAssembly.Document
Dim oOnOff As Boolean = False
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Dim oPDoc As PartDocument = oRefDoc
If oPDoc.ComponentDefinition.Sketches3D.Count > 0 Then
For Each oSketch3D As Sketch3D In oPDoc.ComponentDefinition.Sketches3D
oSketch3D.Visible = oOnOff
Next
End If
End If
Next
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.
If you have time, please... Vote For My IDEAS :light_bulb:and Explore My CONTRIBUTIONS
Inventor 2020 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum
Wesley Crihfield
(Not an Autodesk Employee)
Glad I could help. 🙂
Just in cast you weren't aware though, the codes provided by @el_jefe_de_steak were VBA code. You wouldn't be able to run these directly within an iLogic rule, like the code I posted. To use the VBA code, you would open your VBA Editor [Tools tab > Options panel > VBA Editor] then create a new Module under either your ApplicationProject or under a DocumentProject, then paste that code into that module. (If under the DocumentProject, it will only be available to that document.) Once you have done this the code in that module becomes what is known as a VBA Macro. The Macro will be named according to the name of the Module and the name of the first Public Sub or Function within that module. Then when you are outside of the VBA Editor, you can click the Macros button [Tools tab > Options panel > Macros], which will open a small dialog box. Then in the drop-down list beside "Macros in:", select the Project you placed the Module under. Then all available Modules within that Project will show in the "Macro name:" window above that. Select the one you want, then click Run.
Or, if you're already familiar with all that, then you may also know that you can create a button within you ribbon to represent a macro, and that clicking this button will run the macro. This is what he was referring to at the beginning of the second post.
You can also run iLogic rules from VBA macros, by telling the macro to run the iLogic rule (not by pasting iLogic code into the module). There are several posts here on this forum that can show you how to do this if that's what you were hoping for.
Wesley Crihfield
(Not an Autodesk Employee)
Thanks @WCrihfield . I failed to clarify that in my original post.
Yes, @Anonymous he is correct about this. At our organization we find that it is extremely useful to write macros in VBA instead of iLogic because of the ability to add a control button to the ribbon for easy access. We have a similar macro we use to turn work planes on and off.
If you would like to do this but are unsure how to proceed, I can create a short instructional video demonstrating the process and post it here.
Thank you all for your help and clarification.
We managed to get it working (somehow)
A little clarification:
We are actually accessing inventor via .Net Remoting with C#.
We have an inventor Assembly with many Parts. the whole Assembly, lets say its a machine, can be configured by a Webclient.
We also have the 4 Drawings placed where we set the visibility of each Part, depending on the users configuration.
inside of some Parts are 3D-Sketches, that should also be visible/invisible if the surrounding part is visible/invisible.
But its not working this way. If i hide one part, the 3D-Sketches inside it are still visible.
so i used your code to access the 3D-Sketches directly, but again these dont react to visibility changes, so we have do DELETE the sketches by code. (And undo the deletion it after a DXF-export)
Next problem was to know the name of the Parent-Part in which the 3D-Sketches resides.
the only quick way we could manage this is with the FullFileName wich has a 3digit number in it that we can read.
I know, its a bit error-prone, but we needed a quick solution and it works (for now 😉 )
Here is part of my code:
Console.WriteLine("Searching for 3D-Sketches");
// 3D-Sketches
foreach (Document oRefDoc in oAssyDoc.AllReferencedDocuments)
{
Console.Write(".");
if (oRefDoc.DocumentType == DocumentTypeEnum.kPartDocumentObject)
{
PartDocument oPDoc = (PartDocument)oRefDoc;
if (oPDoc.ComponentDefinition.Sketches3D.Count > 0)
{
String filename = oPDoc.FullFileName;
bool visible = false;
int pos = filename.IndexOf("Parts\\");
if (pos > 0)
{
string clustername = filename.Substring(pos + "Parts\\".Length, 3);
for (int layerNr = 0; layerNr < layersToShow.Length; layerNr++)
{
if (layersToShow[layerNr].ToString("D3").Equals(clustername))
{
visible = true;
break;
}
}
}
foreach (Sketch3D oSketch3D in oPDoc.ComponentDefinition.Sketches3D)
{
for (int i = 1; i <= ActiveSheet.DrawingViews.Count; i++)
{
DrawingView dView = ActiveSheet.DrawingViews[i];
try
{
dView.SetVisibility(oSketch3D, visible);
oSketch3D.Visible = visible;
if (!visible)
{
oSketch3D.Delete();
}
}
catch (Exception e) { }
}
}
}
}
}
Can't find what you're looking for? Ask the community or share your knowledge.