Hide body lines in drawing view by body name

Hide body lines in drawing view by body name

hieut1392
Enthusiast Enthusiast
380 Views
5 Replies
Message 1 of 6

Hide body lines in drawing view by body name

hieut1392
Enthusiast
Enthusiast

I am new to macro. I want to write a macro like title, it is difficult for me, can anyone help me?

 

1) Select desired view

2) Enter body name using input box for example

3) Hide all selected body lines

 

Thanks you in advance.

0 Likes
Accepted solutions (1)
381 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Hi @hieut1392.  This is a relatively common request, but as usual, much more difficult to do by code than it is to do manually.  If you search within this forum, you will likely find several similar discussions very similar to this one.  Below is a link to another similar one I just replied to today:

https://forums.autodesk.com/t5/inventor-programming-ilogic/hide-all-drawing-hidden-lines-that-aren-t... 

 

Below is an example iLogic rule you can test with, to see if it works the way you want it to.

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDDoc As DrawingDocument = TryCast(oInvApp.ActiveDocument, Inventor.DrawingDocument)
	If oDDoc Is Nothing Then Return
	Dim oView As DrawingView = oInvApp.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select a Drawing View.")
	If oView Is Nothing Then Return
	Dim oViewDoc As Inventor.Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
	If (oViewDoc Is Nothing) OrElse (Not TypeOf oViewDoc Is PartDocument) Then Return
	Dim oViewPDoc As PartDocument = oViewDoc
	Dim oBodies As SurfaceBodies = oViewPDoc.ComponentDefinition.SurfaceBodies
	Dim oBodyNames As New List(Of String)
	For Each oBody As SurfaceBody In oBodies
		oBodyNames.Add(oBody.Name)
	Next
	Dim sSelectedBodyName As String = InputListBox("Pick Body Name", oBodyNames, "", "Body Names")
	If sSelectedBodyName Is Nothing OrElse sSelectedBodyName = "" Then Return
	Dim oSelectedBody As SurfaceBody = Nothing
	For Each oBody As SurfaceBody In oBodies
		If oBody.Name = sSelectedBodyName Then
			oSelectedBody = oBody
			Exit For
		End If
	Next
	If oSelectedBody Is Nothing Then Return
	Dim oObjColl As Inventor.ObjectCollection = oInvApp.TransientObjects.CreateObjectCollection
	Dim oDCurves As DrawingCurvesEnumerator = Nothing
	Try : oDCurves = oView.DrawingCurves(oSelectedBody) : Catch : End Try
	If oDCurves Is Nothing OrElse oDCurves.Count = 0 Then Return
	For Each oDCurve As DrawingCurve In oDCurves
		For Each oDCS As DrawingCurveSegment In oDCurve.Segments
			oObjColl.Add(oDCS)
		Next oDCS
	Next oDCurve
	Dim oTrans As Inventor.Transaction = oInvApp.TransactionManager.StartTransaction(oDDoc, "Hide Body Curves - iLogic")
	oDDoc.SelectSet.SelectMultiple(oObjColl)
	oInvApp.CommandManager.ControlDefinitions("DrawingEntityVisibilityCtxCmd").Execute
	oTrans.End
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

WCrihfield
Mentor
Mentor

Manually, you can look in the model browser tree of your drawing, expand the browser node of that specific view, then keep expanding child nodes until you can see the individual bodies.  Then right-click on the browser node for that body, and choose 'Select As Edges'.

WCrihfield_1-1732213143275.png

Then, all the lines (drawing curves) belonging to that body within that view will be selected.  Then right-click again somewhere out in the open area of your drawing sheet, and choose 'Visibility'.

WCrihfield_2-1732213273896.png  Then boom, all those lines in the view are hidden.  And if you want them to show again, that is also fairly easy to do manually.  Just select the view in the sheet area, then right-click, and choose 'Show Hidden Edges'.

WCrihfield_3-1732213462487.png  Then, it will have those lines showing in red, and will be waiting for you to either choose which specific ones to show, or right-click again, and choose 'Show All'.

WCrihfield_4-1732213574755.png

 

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 6

hieut1392
Enthusiast
Enthusiast

First of all, thank you for your help, I think I'm almost there, I'm just stuck at this point, I hope you can help me.

I found a code on the forum to hide the body by picking the edge of the body like this (code number 1) :

 

 

Dim oOccioV As DrawingCurveSegment
Set oOccioV = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kDrawingCurveSegmentFilter, "Pick edge")

Dim oView As DrawingView
Set oView = oOccioV.Parent.Parent
    
Dim oToccoH As Object
Set oToccoH = oOccioV.Parent.ModelGeometry.Parent
    
Call oView.SetVisibility(oToccoH, False)

 

 

However, the object is declared as Drawing Curve Segment. Your code runs perfectly ok, however, I want to hide the body like right-clicking on the body and selecting Visibility. Is there a way to get the Drawing Curve Segment of a body whose name is known? I think I can combine your code and the code on the forum.

 

 

Sub Main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oDDoc As DrawingDocument = TryCast(oInvApp.ActiveDocument, Inventor.DrawingDocument)
	If oDDoc Is Nothing Then Return
	Dim oView As DrawingView = oInvApp.CommandManager.Pick(SelectionFilterEnum.kDrawingViewFilter, "Select a Drawing View.")
	If oView Is Nothing Then Return
	Dim oViewDoc As Inventor.Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
	If (oViewDoc Is Nothing) OrElse (Not TypeOf oViewDoc Is PartDocument) Then Return
	Dim oViewPDoc As PartDocument = oViewDoc
	Dim oBodies As SurfaceBodies = oViewPDoc.ComponentDefinition.SurfaceBodies
	Dim oBodyNames As New List(Of String)
	For Each oBody As SurfaceBody In oBodies
		oBodyNames.Add(oBody.Name)
	Next
	Dim sSelectedBodyName As String = InputListBox("Pick Body Name", oBodyNames, "", "Body Names")
	If sSelectedBodyName Is Nothing OrElse sSelectedBodyName = "" Then Return
	Dim oSelectedBody As SurfaceBody = Nothing
	For Each oBody As SurfaceBody In oBodies
		If oBody.Name = sSelectedBodyName Then
			oSelectedBody = oBody
			Exit For
		End If
	Next
	If oSelectedBody Is Nothing Then Return
'CODE NUMBER 1 HERE

End Sub

 

 

 

 

0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @hieut1392.  That is a nice thought, but I don't think it will be practical.  That DrawingView.SetVisibility method can only work on one entity at a time, and a SurfaceBody object does not appear to be one of the objects that we can provide to that method directly.  (I did do some testing to make sure.)  One of the most challenging points in a code like that first one, is where it encounters the DrawingCurve.ModelGeometry property, because its value is just a generic Object type.  That property has that type of value because it can return many different types of objects, depending on what type of document was directly referenced in the view, and what that specific piece of view geometry was based on.  It could be sketch geometry, or work feature, assembly component geometry, assembly proxy stuff, curved outer surface profile/silhouette, etc.  Likely the most common thing to expect, when the view is of a part, is an Edge object.  If that is the case, then its 'parent' will be a SurfaceBody object.  So, if you want to 'pick' the edge of the body, instead of specify the body by its name, then that part would be possible.  But the part of the code where we turn the visibility of all the geometry in the view belonging to that body, is another story, and there are just a few options that I have ever seen.  Setting each DrawingCurveSegment.Visible property to False (takes a long time), gathering them all into an ObjectCollection, then either select them and execute the command on them, or use the Sheet.ChangeLayer method on them.  When doing the ChangeLayer route, we can create a new custom layer, then set its visibility to False, then set that geometry to that Layer.  It's not the easiest to undo later though if needed, if you have done other things after that happened.

 

Unfortunately, we can not really modify the right-click menu to include additional functionality, without some serious complication, such as an Inventor add-in, because it involves customizing Inventor's user interface, and monitoring the 'events' of those custom things we add into it with our own event handlers, to support their functionality with our own code.

 

Have you considered:  Create a custom DVR (DesignViewRepresentation) in the part, make sure it is 'active', then turn off the visibility of that body, so that DVR will record it, then save the part file.  Then in the drawing of that part, set the view of that part to that DVR.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 6

hieut1392
Enthusiast
Enthusiast

I stumbled upon your code in this post, it does exactly what I wanted.

 

https://forums.autodesk.com/t5/inventor-programming-ilogic/hide-all-other-solid-keep-selected-visibi...

 

I edited it a bit, the vba code is below for those who need it.

 

 

Sub Main()
    Dim oObj As Object
    Set oObj = ThisApplication.CommandManager.Pick(kDrawingViewFilter, "Select a Drawing View.")
    Dim oView As DrawingView
    Set oView = oObj
    Dim oModelDoc As Document
    Set oModelDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
    Dim oPDoc As PartDocument
    Set oPDoc = oModelDoc
    Dim oBodies As SurfaceBodies
    Set oBodies = oPDoc.ComponentDefinition.SurfaceBodies
    Dim oBodyNames As Collection
    Set oBodyNames = New Collection
    Dim oBody As SurfaceBody
    For Each oBody In oBodies
        oBodyNames.Add oBody.name
    Next
    Dim oBodyName As String
    oBodyName = InputBox("Body Names = ", "Input A Body Name", "P")
    If oBodyName = "" Then Exit Sub
    For Each oBody In oBodies
        If oBody.name <> oBodyName Then
            oView.SetVisibility oBody, False
        End If
    Next
    oView.Parent.Update 'update the sheet
    oView.Parent.Parent.Update ' update the drawing
End Sub

 

 


0 Likes