Hiding all work features in an assembly

Hiding all work features in an assembly

Anonymous
Not applicable
3,073 Views
12 Replies
Message 1 of 13

Hiding all work features in an assembly

Anonymous
Not applicable

Hello,

 

I'm trying to hide any visible work features in assemblies. Both assembly and component parts have multiple model states.

 

I found this post on GitHub, but even implementing it exactly as-is causes an error to be thrown when trying to set a visible work feature's `.Visible` to false.

 

Is there anyone able to help me understand the dynamics of this process?

 

Thanks

0 Likes
3,074 Views
12 Replies
Replies (12)
Message 2 of 13

WCrihfield
Mentor
Mentor

Hi @Anonymous.  Visibility of things like work features is controlled by the DesignViewRepresentation (DVR) objects in the DesignViewRepresentations (DVR's) collection of each document.  Their main purpose is to record your settings for the visibility of things, record their colors/appearances, save view angles, save section view set-ups, and that sort of thing.  When you put a document into an assembly, represented by an assembly component object, you can set the DVR of that component to one that exists within the document it represents, and you can make that assigned DVR 'associative', which means that if changes are made to the model that effect that specific DVR, those changes will immediately effect that component.  If that 'associative' setting is turned on, it will warn you about making changes to that component that may break that relationship, and if you agree to go ahead, it will usually break that relationship, meaning that your component will no longer stay up to date with visual changes made to the model it represents.  If you have that 'associative' setting turned off for all your components, you shouldn't have much trouble changing visibility settings within components, but they will no longer stay up to date with any new visibility related changes made to the source model.

 

When ModelStates are involved, that mainly just complicates the process of making sure you are referencing the right version of the document, but the ModelState does not record things like the visibility of work features.  You just need to make sure you are working with the 'factory' version of those documents that have custom ModelStates (more than the one original ModelState, which is usually named "Master" or "Primary").  The 'factory' version of the document is simply the version of that document that is under the influence of that document's 'active' ModelState.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 13

Anonymous
Not applicable
I'm not sure I follow. Just to be absolutely sure we're talking about the same thing, I don't need to know anything about what the part files are doing, but for an active file, I need the work features to be hidden.

In an assembly, do I need to access the DVR of the part files? And then what do I do with it? I can't seem to find my way to the right properties.

So if the model state doesn't record the visibility of the work features, theoretically I would change it in the DVR of the factory document and it would propagate to each?
0 Likes
Message 4 of 13

WCrihfield
Mentor
Mentor

It's complicated to explain everything you might need to know about all of it right now by just typing up a bunch of stuff here in this post, so instead I'm just going to supply you with the most basic iLogic code for the most basic process for this task.  This does not take any DVR settings into account, so it may not work the way you want, but if you are not familiar with them, they you may not have used them for anything before, which may make this work OK for you.  So, if every assembly component, at every level of your entire assembly structure is still set to its default 'Master' DVR, then I do not foresee any reason why this code would not work OK for you.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	HideAllWorkFeatures(oADoc)
	Dim oRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments
	If oRefDocs.Count = 0 Then Exit Sub
	For Each oRefDoc As Document In oRefDocs
		HideAllWorkFeatures(oRefDoc)
	Next
	If oADoc.RequiresUpdate Then oADoc.Update2(True)
	If oADoc.Dirty Then oADoc.Save2
	MsgBox("Done Hiding All Work Features In All Referenced Documents.", vbInformation, "Done")
End Sub

Sub HideAllWorkFeatures(oDoc As Document)
	If IsNothing(oDoc) OrElse oDoc.IsModifiable = False Then Exit Sub
	If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject And _
		oDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
		Exit Sub
	End If
	Dim oWAs As WorkAxes = oDoc.ComponentDefinition.WorkAxes
	Dim oWPs As WorkPlanes = oDoc.ComponentDefinition.WorkPlanes
	Dim oWPts As WorkPoints = oDoc.ComponentDefinition.WorkPoints
	Dim oWSs As WorkSurfaces = oDoc.ComponentDefinition.WorkSurfaces
	For Each oWA As WorkAxis In oWAs
		If oWA.Visible Then oWA.Visible = False
	Next
	For Each oWP As WorkPlane In oWPs
		If oWP.Visible Then oWP.Visible = False
	Next
	For Each oWPt As WorkPoint In oWPts
		If oWPt.Visible Then oWPt.Visible = False
	Next
	For Each oWS As WorkSurface In oWSs
		If oWS.Visible Then oWS.Visible = False
	Next
	If oDoc.RequiresUpdate Then oDoc.Update2(True)
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 13

Maxim-CADman77
Advisor
Advisor

Thread is not new yet not marked as solved ... thus ...

... maybe you need just this single line?

 

 

ThisApplication.CommandManager.ControlDefinitions("AppAllWorkfeaturesCmd").Execute

 

 

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 6 of 13

Anonymous
Not applicable

Sadly I'm not sure this solves the problem, as this seems to be a master switch which simply overrides the visibility setting per work feature. What is needed is a way to toggle each individually, so as to allow them to be turned on again, one by one, rather than returning to the original state. @WCrihfield may have the solution, but I haven't had time to implement it to see if it works.

0 Likes
Message 7 of 13

WCrihfield
Mentor
Mentor

I believe the ControlDefinition mentioned above is the one seen in the following location...

While working within a part or assembly, if you go to the View tab, then you will see a control called Object Visibility on the Visibility panel.  It has a drop-down arrow beside it.  When you click that down arrow, you will see a list of object types that you can turn on or off, by checking or un-checking checkboxes beside them.  At the very top of that list is All Workfeatures.  Executing that command likely does the same as toggling that control on/off.  This appears to be a 'per document' type of setting, instead of a per feature setting, or an application wide setting, because I can have two different parts open in different document tabs (that have nothing to do with each other), and can have some WorkPlanes showing in both.  Then turn that setting off while one part is active on my screen, then when I go to the other part (and make it active), those settings are still on within that control drop-down, and its WorkPlanes are still visible.  I am not sure if that setting (the state of that control) is saved per document or not though, since it is not actually applied directly to the individual instances.  If the WorkPlane instances already have their visibility turned off individually, then turning that ribbon control on does not turn on the visibility of all my work features...only the ones that were already set to be visible individually.  So, that ribbon control is only good for temporarily hiding the work features that have not already been hidden individually, but not good for unhiding the ones that were already hidden individually.

 

When you have a Part open, there is usually model browser node near the top of the model browser tree, with its name starting with "View:", followed by the name of the active DesignViewRepresentation (DVR).  Most of the time, there is only the one, Locked DVR named "[Primary]", and no 'custom' ones, until we create some custom ones.  We often do not need to use these in a part, but sometimes we do, if we want to show the same part in multiple colors / appearances, or if the part has multiple solid bodies, and sometimes we want to be able to turn the visibility of some solid bodies on/off.  So, when we drop a part into an assembly, as an assembly component, we can have multiple copies of that same part in the same assembly.  And if that part has multiple DVR's defined within it, we can set one assembly component to one of those DVR's (Red, for example, or certain bodies visible), and set another assembly occurrence of that same part to a different DVR (Blue, for example, or different bodies visible).  The same is true when adding a sub assembly into another assembly, when the sub assembly may have multiple DVRs, and we add multiple instances of that sub assembly into the other assembly, as assembly components.  We can set one sub assembly occurrence to one DVR (some of its components visible, some certain colors), while setting another instance of that sub assembly component to a different DVR (different components visible, other component colors).  However, the DVR's in sub assemblies record much more.  Each DVR in each sub assembly will not only record the visibility, colors/appearances settings defined directly in that sub assembly, but will also record which DVR each individual component within that sub assembly is set to.  This is why these should be set-up from the bottom, to the top, instead of from the top, to the bottom.

 

Because all visibility (not suppression) and color, and appearances are recorded by DVR's, when we want to control those aspects within the documents that assembly components represent, the proper way to do that is by having a DVR defined within the referenced document and set up the way we want it, then setting the assembly component referencing that document to the DVR that is defined within it.  And, because of this, it is always best to define these settings along the way, as we design the parts and sub assemblies, so that all the DVR's we may want to use at a higher level assembly, will already exist and be ready to use.  If we choose not create/define any DVR's along the way, then controlling all those things from the top, down, from the main assembly later becomes much more complicated and more difficult to do properly.

 

On the other hand, if none of the parts, and none of the sub assemblies ever had any custom DVR's created, and all of them were still just set to their original DVR (named "[Primary]"), then a relatively simple code process like the one I posted above may work OK for you.  This would be because...all those types of settings (visibility, colors, appearances) that were set along the way, will all have been recorded by that one, default DVR, and any changes made by the code will also be recorded by that one, default DVR.

 

The following properties & methods of the assembly component (ComponentOccurrence) can be used to get/set the DVR that an individual assembly component is currently set to.

ComponentOccurrence.ActiveDesignViewRepresentation 

ComponentOccurrence.IsAssociativeToDesignViewRepresentation 

ComponentOccurrence.SetDesignViewRepresentation 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 13

srinivasanyl
Contributor
Contributor

Hi, I used this code in assembly, all the orgin planes, axis, points visibilty turned off. but user planes, axis, points not visibility turned off. anybody please for update me.

0 Likes
Message 9 of 13

srinivasanyl
Contributor
Contributor
Sub Main
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
HideAllWorkFeatures(oADoc)
Dim oRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments
If oRefDocs.Count = 0 Then Exit Sub
For Each oRefDoc As Document In oRefDocs
HideAllWorkFeatures(oRefDoc)
Next
If oADoc.RequiresUpdate Then oADoc.Update2(True)
If oADoc.Dirty Then oADoc.Save2
MsgBox("Done Hiding All Work Features In All Referenced Documents.", vbInformation, "Done")
End Sub

Sub HideAllWorkFeatures(oDoc As Document)
If IsNothing(oDoc) OrElse oDoc.IsModifiable = False Then Exit Sub
If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject And _
oDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
Exit Sub
End If
Dim oWAs = oDoc.ComponentDefinition.WorkAxes
Dim oWPs = oDoc.ComponentDefinition.workPlanes
Dim oWPts = oDoc.ComponentDefinition.WorkPoints

'oDoc = ThisApplication.ActiveDocument
'Dim oWSs As WorkSurfaces = oDoc.ComponentDefinition.WorkSurfaces
For Each oWA As WorkAxis In oDoc.ComponentDefinition.WorkAxes
If oWA.Visible Then oWA.Visible = False
Next
For Each oWP In oDoc.ComponentDefinition.WorkPlanes
If oWP.Visible Then oWP.Visible = False
Next
For Each oWPt In oDoc.ComponentDefinition.WorkPoints
If oWPt.Visible Then oWPt.Visible = False
Next
'For Each oWS As WorkSurface In oWSs
' If oWS.Visible Then oWS.Visible = False
'Next
If oDoc.RequiresUpdate Then oDoc.Update2(True)
End Sub
0 Likes
Message 10 of 13

Curtis_W
Consultant
Consultant
0 Likes
Message 11 of 13

tim11_manhhieu
Advocate
Advocate

hi  @WCrihfield 

i followed your code to hide all User Coordinate System but it is not working, could you take a look my code, is there something wrong?

 

tim11_manhhieu_0-1740387409098.png

 

 

 

Option Explicit

Sub Main()

    Dim oADoc As AssemblyDocument
    Set oADoc = ThisApplication.ActiveDocument
    
    Call HideAllWorkFeatures(oADoc)
    
    Dim oRefDocs As DocumentsEnumerator
    Set oRefDocs = oADoc.AllReferencedDocuments
    
    If oRefDocs.count = 0 Then Exit Sub
    Dim oRefDoc As Document
    
    For Each oRefDoc In oRefDocs
        Call HideAllWorkFeatures(oRefDoc)
    Next
    If oADoc.RequiresUpdate Then oADoc.Update2 (True)
    If oADoc.Dirty Then oADoc.Save2
End Sub

Sub HideAllWorkFeatures(oDoc As Document)
    If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject And _
        oDoc.DocumentType <> DocumentTypeEnum.kPartDocumentObject Then
        Exit Sub
    End If
    
    Dim oWAs As WorkAxes
    Set oWAs = oDoc.ComponentDefinition.WorkAxes
    
    Dim oWPs As WorkPlanes
    Set oWPs = oDoc.ComponentDefinition.WorkPlanes
    
    Dim oWPts As WorkPoints
    Set oWPts = oDoc.ComponentDefinition.WorkPoints
    
    Dim oUCSs As UserCoordinateSystems
    Set oUCSs = oDoc.ComponentDefinition.UserCoordinateSystems
   
    Dim oWA As WorkAxis
    Dim oWP As WorkPlane
    Dim oWPt As WorkPoint
    Dim oUCS As UserCoordinateSystem
    
    For Each oWA In oWAs
        If oWA.Visible Then oWA.Visible = False
    Next
    For Each oWP In oWPs
        If oWP.Visible Then oWP.Visible = False
    Next
    For Each oWPt In oWPts
        If oWPt.Visible Then oWPt.Visible = False
    Next
    
    For Each oUCS In oUCSs
        If oUCS.Visible Then oUCS.Visible = False
    Next

    If oDoc.RequiresUpdate Then oDoc.Update2 (True)
End Sub

 

 

 

Preview
 
 
 
0 Likes
Message 12 of 13

WCrihfield
Mentor
Mentor

Hi @tim11_manhhieu.

  • Was there an error dialog shown when you ran it?
    • If so, then what did the error say, and did the error dialog indicate which line of the code the error originated from?
  • Did it change the visibility of any other work features in those same documents, but just not the UCS's?
  • Do those specific documents contain multiple ModelStates?
  • Are they associated with the Content Center in any way?
  • Are they iAssembly or iPart members?

The code looks OK in basic functionality, but lacks any error prevention/avoidance/handling, such as utilizing the Try...Catch...End Try statement.  I would suggest eliminating the Document.RequiresUpdate Property check, and just do the Update anyways.  I know I had it in the codes I posted above, but I have changed that in my working codes not too long after posting those.  It doesn't seem to be effected by changes that are 'visual only' in nature, so not really that appropriate in this situation unless, other things have also been moved or changed also.  The View.Update method may be a bit more appropriate here.

Anyways, I ask about some of those things above, because they can cause the model to be treated as 'ReadOnly' by the Inventor API, even if the File they are associate with may not be set to ReadOnly.  Usually those types of issues can be caught or avoided by checking the Document.IsModifiable property, which you can see being used in some of the earlier code examples.  However, when the document is not modifiable due to ModelStates, then we can sometimes use some additional code to find a way to work around that issue, but not always.  As dynamic and beneficial as they may be, when ModelStates get involved, they do tend to add a whole additional layer of complexity into automating Inventor by code.

Edit:  Since your code appears to be in VBA, instead of vb.net, the Try...Catch...End Try statement would not be available, so you may need to use the 'On Error Resume Next' code phrase instead, to help avoid errors.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 13 of 13

tim11_manhhieu
Advocate
Advocate

thank you for your detailed replay, i figure it out.

0 Likes