Hi all,
I'm looking for a code, that will work in a drawing view. I need to hide all solids in all parts in the assembly and subassemblies in that view, that have a "-" in the name (-hole). So it should go through all the parts and all assemblies in that model view and turn off visibility of any solid that have a "-" in the name (or probably easier exact name "-hole").
I did find a code, that kind of works for single part, but I dont know how to make it for assembly and all parts in it. And also the list of solid names is not necessary for me. Here is that code from WCrihfield:
Dim oDDoc As DrawingDocument = ThisDoc.Document Dim oView As DrawingView = oDDoc.ActiveSheet.DrawingViews.Item(1) Dim oModel As Document = oView.ReferencedDocumentDescriptor.ReferencedDocument If oModel.DocumentType = DocumentTypeEnum.kPartDocumentObject Then Dim oPDoc As PartDocument = oModel If oPDoc.ComponentDefinition.HasMultipleSolidBodies = False Then Exit Sub Dim oBodies As SurfaceBodies = oPDoc.ComponentDefinition.SurfaceBodies Dim oBody3 As SurfaceBody = oBodies.Item(3) Dim oBodyGeomInView As DrawingCurvesEnumerator = oView.DrawingCurves(oBody3) If oBodyGeomInView.Count = 0 Then Exit Sub For Each oDC As DrawingCurve In oBodyGeomInView For Each oDCS As DrawingCurveSegment In oDC.Segments oDCS.Visible = False Next Next End If
Thank you all
Solved! Go to Solution.
Solved by martin.jerabekM5BZE. Go to Solution.
Solved by Andrii_Humeniuk. Go to Solution.
Hi @martin.jerabekM5BZE . The code looks for the character "-" in the body name (line 24).
Sub main
Dim oInvApp As Inventor.Application = ThisApplication
Dim oDDoc As DrawingDocument = oInvApp.ActiveDocument
Dim oTM As TransactionManager = oInvApp.TransactionManager
Dim newTM As Transaction = oTM.StartTransaction(oDDoc, "ChangeCurvesColor")
For Each oSheet As Sheet In oDDoc.Sheets
For Each oView As DrawingView In oSheet.DrawingViews
If oView.ViewStyle = DrawingViewStyleEnum.kShadedDrawingViewStyle Then Continue For
Dim oRefDoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument
Dim oRefDef As AssemblyComponentDefinition = oRefDoc.ComponentDefinition
Call ChangeCutCurvesView(oView, oRefDef.Occurrences)
Next
Next
newTM.End()
End Sub
Private Function ChangeCutCurvesView(ByVal oView As DrawingView, ByVal oOccs As ComponentOccurrences)
For Each oOcc As ComponentOccurrence In oOccs
If TypeOf oOcc.Definition Is PartComponentDefinition Or _
TypeOf oOcc.Definition Is SheetMetalComponentDefinition Then
Dim oCompDef As ComponentDefinition = oOcc.Definition
If oOcc.SurfaceBodies.Count > 1 Then
For Each oBody As SurfaceBody In oOcc.SurfaceBodies
If oBody.Name.Contains("-") Then
oCurves = oView.DrawingCurves(oBody)
For Each oCurve As DrawingCurve In oCurves
For Each oSegment As DrawingCurveSegment In oCurve.Segments
oSegment.Visible = False
Next
Next
End If
Next
End If
End If
If oOcc.SubOccurrences IsNot Nothing Then
Call ChangeCutCurvesView(oView, oOcc.SubOccurrences)
End If
Next
End Function
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
Hello Andrii_Humeniuk,
thank you for the code. But I'm having an error message when I'm trying to run it. Does it work for you? How do you proceed? (slelect view and run the rule? I have Inventor 2023 latest update.
Thank you
In my case the code works fine. I added an "On Error Resume Next" statement (line 27), if that doesn't help you, you'll have to catch the error to figure out what the problem is.
Sub main
Dim oInvApp As Inventor.Application = ThisApplication
Dim oDDoc As DrawingDocument = oInvApp.ActiveDocument
Dim oTM As TransactionManager = oInvApp.TransactionManager
Dim newTM As Transaction = oTM.StartTransaction(oDDoc, "ChangeCurvesColor")
For Each oSheet As Sheet In oDDoc.Sheets
For Each oView As DrawingView In oSheet.DrawingViews
If oView.ViewStyle = DrawingViewStyleEnum.kShadedDrawingViewStyle Then Continue For
Dim oRefDoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument
Dim oRefDef As AssemblyComponentDefinition = oRefDoc.ComponentDefinition
Call ChangeCutCurvesView(oView, oRefDef.Occurrences)
Next
Next
newTM.End()
End Sub
Private Function ChangeCutCurvesView(ByVal oView As DrawingView, ByVal oOccs As ComponentOccurrences)
For Each oOcc As ComponentOccurrence In oOccs
If TypeOf oOcc.Definition Is PartComponentDefinition Or _
TypeOf oOcc.Definition Is SheetMetalComponentDefinition Then
Dim oCompDef As ComponentDefinition = oOcc.Definition
If oOcc.SurfaceBodies.Count > 1 Then
For Each oBody As SurfaceBody In oOcc.SurfaceBodies
If oBody.Name.Contains("-") Then
oCurves = oView.DrawingCurves(oBody)
For Each oCurve As DrawingCurve In oCurves
On Error Resume Next
For Each oSegment As DrawingCurveSegment In oCurve.Segments
oSegment.Visible = False
Next
Next
End If
Next
End If
End If
If oOcc.SubOccurrences IsNot Nothing Then
Call ChangeCutCurvesView(oView, oOcc.SubOccurrences)
End If
Next
End Function
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
OK I did a test and it works for normal parts. But it does not hide lines in an ISO view. Why it doesn't work for ISO?
And that error I'm having is related to iParts. Lots of my parts are an iParts. Can that code be adapted to do all, normal parts and iParts?
So this code works actually with hiding the lines of those solids. Wouldn't be a code with changing visibility of those solids in that view easier?
Thank you
OK, I removed that part of code with specification of view style, as it is not requested and it should work for all types. But the problem with iPart solids stays.
Code:
Sub main Dim oInvApp As Inventor.Application = ThisApplication Dim oDDoc As DrawingDocument = oInvApp.ActiveDocument Dim oTM As TransactionManager = oInvApp.TransactionManager Dim newTM As Transaction = oTM.StartTransaction(oDDoc, "ChangeCurvesColor") For Each oSheet As Sheet In oDDoc.Sheets For Each oView As DrawingView In oSheet.DrawingViews Dim oRefDoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument Dim oRefDef As AssemblyComponentDefinition = oRefDoc.ComponentDefinition Call ChangeCutCurvesView(oView, oRefDef.Occurrences) Next Next newTM.End() End Sub Private Function ChangeCutCurvesView(ByVal oView As DrawingView, ByVal oOccs As ComponentOccurrences) For Each oOcc As ComponentOccurrence In oOccs If TypeOf oOcc.Definition Is PartComponentDefinition Or _ TypeOf oOcc.Definition Is SheetMetalComponentDefinition Then Dim oCompDef As ComponentDefinition = oOcc.Definition If oOcc.SurfaceBodies.Count > 1 Then For Each oBody As SurfaceBody In oOcc.SurfaceBodies If oBody.Name.Contains("-") Then oCurves = oView.DrawingCurves(oBody) For Each oCurve As DrawingCurve In oCurves On Error Resume Next For Each oSegment As DrawingCurveSegment In oCurve.Segments oSegment.Visible = False Next Next End If Next End If End If If oOcc.SubOccurrences IsNot Nothing Then Call ChangeCutCurvesView(oView, oOcc.SubOccurrences) End If Next End Function
Still I'd rather change visibility of actual solids, as hiding lines sometimes makes areas without lines on an ISO views.
Thank you very much for your help
Try this code:
Sub main
Dim oInvApp As Inventor.Application = ThisApplication
Dim oDDoc As DrawingDocument = oInvApp.ActiveDocument
Dim oTM As TransactionManager = oInvApp.TransactionManager
Dim newTM As Transaction = oTM.StartTransaction(oDDoc, "ChangeCurvesColor")
For Each oSheet As Sheet In oDDoc.Sheets
For Each oView As DrawingView In oSheet.DrawingViews
Dim oRefDoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument
Dim oRefDef As AssemblyComponentDefinition = oRefDoc.ComponentDefinition
Call ChangeCutCurvesView(oView, oRefDef.Occurrences)
Next
Next
newTM.End()
End Sub
Private Function ChangeCutCurvesView(ByVal oView As DrawingView, ByVal oOccs As ComponentOccurrences)
For Each oOcc As ComponentOccurrence In oOccs
If TypeOf oOcc.Definition Is PartComponentDefinition Or _
TypeOf oOcc.Definition Is SheetMetalComponentDefinition Then
Dim oCompDef As ComponentDefinition = oOcc.Definition
If oOcc.SurfaceBodies.Count > 1 Then
For Each oBody As SurfaceBody In oOcc.SurfaceBodies
If oBody.Name.Contains("-") Then
oView.SetVisibility(oBody, False)
End If
Next
End If
End If
If oOcc.SubOccurrences IsNot Nothing Then
Call ChangeCutCurvesView(oView, oOcc.SubOccurrences)
End If
Next
End Function
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
Thank you,
nice, that works almost flawlessly. I have an assembly with suppressed parts and that obviously causes an error as I tried to run that code with an assembly without suppressed parts and there it works great. Can you add to that code a line, were it will not go through suppressed parts? That could hopefully help.
Is there any possibility to dhave 2 cases of running?
1. Not selected a view - so it goes through all views on the list
2. Selected a single view - it hides just solids on that selected view
Thank you, you are genius
No problem... Try this code:
Sub main
Dim oInvApp As Inventor.Application = ThisApplication
Dim oDDoc As DrawingDocument = oInvApp.ActiveDocument
Dim oTM As TransactionManager = oInvApp.TransactionManager
Dim vbResult As DialogResult = MessageBox.Show("Choose a separate view?", _
"Chooseview", _
MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button1)
If vbResult = vbCancel Then Exit Sub
Dim newTM As Transaction = oTM.StartTransaction(oDDoc, "ChangeCurvesColor")
If vbResult = vbYes Then
Dim oView As DrawingView
oView = oInvApp.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select view...")
If oView Is Nothing Then Exit Sub
Dim oRefDoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument
Dim oRefDef As AssemblyComponentDefinition = oRefDoc.ComponentDefinition
Call ChangeCutCurvesView(oView, oRefDef.Occurrences)
Else
For Each oSheet As Sheet In oDDoc.Sheets
For Each oView As DrawingView In oSheet.DrawingViews
Dim oRefDoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument
Dim oRefDef As AssemblyComponentDefinition = oRefDoc.ComponentDefinition
Call ChangeCutCurvesView(oView, oRefDef.Occurrences)
Next
Next
End If
newTM.End()
End Sub
Private Function ChangeCutCurvesView(ByVal oView As DrawingView, ByVal oOccs As ComponentOccurrences)
For Each oOcc As ComponentOccurrence In oOccs
If oOcc.Suppressed Then Continue For
If TypeOf oOcc.Definition Is PartComponentDefinition Or _
TypeOf oOcc.Definition Is SheetMetalComponentDefinition Then
Dim oCompDef As ComponentDefinition = oOcc.Definition
If oOcc.SurfaceBodies.Count > 1 Then
For Each oBody As SurfaceBody In oOcc.SurfaceBodies
If oBody.Name.Contains("-") Then
oView.SetVisibility(oBody, False)
End If
Next
End If
End If
If oOcc.SubOccurrences IsNot Nothing Then
Call ChangeCutCurvesView(oView, oOcc.SubOccurrences)
End If
Next
End Function
Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor
LinkedIn | My free Inventor Addin | My Repositories
Did you find this reply helpful ? If so please use the Accept as Solution/Like.
Awesome!! One more thing as a safety. When the Design view is not set to Primary but another and set to Associative, a bug error appears. Can be in the code written, that it check if the Design view is associative and if yes, then cancels this associativity first and continue?
Thank you very much for your help.
Final code is here: It changes Design View to not Associative before it hides those solids. If the view was Associative, it produced error.
Sub main Dim oInvApp As Inventor.Application = ThisApplication Dim oDDoc As DrawingDocument = oInvApp.ActiveDocument Dim oTM As TransactionManager = oInvApp.TransactionManager Dim vbResult As DialogResult = MessageBox.Show("Choose a separate view?", _ "Chooseview", _ MessageBoxButtons.YesNoCancel, _ MessageBoxIcon.Question, _ MessageBoxDefaultButton.Button1) If vbResult = vbCancel Then Exit Sub Dim newTM As Transaction = oTM.StartTransaction(oDDoc, "ChangeCurvesColor") If vbResult = vbYes Then Dim oView As DrawingView oView = oInvApp.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select view...") If oView Is Nothing Then Exit Sub Dim oRefDoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument Dim oRefDef As AssemblyComponentDefinition = oRefDoc.ComponentDefinition If oView.ReferencedDocumentDescriptor.ReferencedDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Dim name As String: name = oView.DesignViewRepresentation Call oView.SetDesignViewRepresentation(name, False) End If Call ChangeCutCurvesView(oView, oRefDef.Occurrences) Else For Each oSheet As Sheet In oDDoc.Sheets For Each oView As DrawingView In oSheet.DrawingViews Dim oRefDoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument Dim oRefDef As AssemblyComponentDefinition = oRefDoc.ComponentDefinition If oView.ReferencedDocumentDescriptor.ReferencedDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then Dim name As String: name = oView.DesignViewRepresentation Call oView.SetDesignViewRepresentation(name, False) End If Call ChangeCutCurvesView(oView, oRefDef.Occurrences) Next Next End If newTM.End() End Sub Private Function ChangeCutCurvesView(ByVal oView As DrawingView, ByVal oOccs As ComponentOccurrences) For Each oOcc As ComponentOccurrence In oOccs If oOcc.Suppressed Then Continue For If TypeOf oOcc.Definition Is PartComponentDefinition Or _ TypeOf oOcc.Definition Is SheetMetalComponentDefinition Then Dim oCompDef As ComponentDefinition = oOcc.Definition If oOcc.SurfaceBodies.Count > 1 Then For Each oBody As SurfaceBody In oOcc.SurfaceBodies If oBody.Name.Contains("-") Then oView.SetVisibility(oBody, False) End If Next End If End If If oOcc.SubOccurrences IsNot Nothing Then Call ChangeCutCurvesView(oView, oOcc.SubOccurrences) End If Next End Function
Hi, how dificult is to add to the code, that it works also for views from ipn (presentation) files? Thank you
Can't find what you're looking for? Ask the community or share your knowledge.