- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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:
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
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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'.
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'.
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'.
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'.
Wesley Crihfield
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
(Not an Autodesk Employee)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I stumbled upon your code in this post, it does exactly what I wanted.
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