Part Isolation and View Representation Automation

Part Isolation and View Representation Automation

get2dpk
Participant Participant
1,370 Views
12 Replies
Message 1 of 13

Part Isolation and View Representation Automation

get2dpk
Participant
Participant

Hi Guys.

I was thinking to isolate the Parts in an assembly and save them as Each representations

for Example, the Below Assy has 17 ipts

so when the automation is run, each parts are to be isolated and 17 view representations are created 

get2dpk_1-1664165070450.png

 

for part 1 

get2dpk_4-1664165359531.png

similary it has to create number of View representations based on number of IPTS

Could anyone help in creating Ilogic/VBA code for Automating this process..

 

Thanks in Advance !!

 

0 Likes
1,371 Views
12 Replies
Replies (12)
Message 2 of 13

dalton98
Collaborator
Collaborator

This should do what you wanted. If you want to have multiple components in the same representation view (i.e. duplicates) then you would have to do it differently

Dim oAssDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oReps As DesignViewRepresentations
oReps = oAssDoc.ComponentDefinition.RepresentationsManager.DesignViewRepresentations
Dim oCtrlDefs As ControlDefinitions
oCtrlDefs = ThisApplication.CommandManager.ControlDefinitions

'sort trough each component in the top level assembly
Dim oOcc As ComponentOccurrence
For Each oOcc In oAssDoc.ComponentDefinition.Occurrences
	oReps.Item("Master").Activate
	
	'create representations
	If oOcc.Suppressed = True Then Continue For
	Try
	oReps.Item(oOcc.Name).Activate
	Catch
	oReps.Add(oOcc.Name)
	oReps.Item(oOcc.Name).Activate
	End Try
	
	oAssDoc.SelectSet.Select(oOcc)
	oCtrlDefs.Item("AssemblyIsolateCmd").Execute
	oAssDoc.SelectSet.Clear
	
Next
0 Likes
Message 3 of 13

get2dpk
Participant
Participant

hi @dalton98 

Thanks for the Code, it works correctly as I expected.

Still as an improvement, design views are created for  each and every parts.

Is there a way we can avoid duplicate  creation of design view Representations if same IPT/IAM is used more than once,
for eg, in picture below,  one desing View representation is sufficient for LO-096581-01(used 7 times)

get2dpk_1-1664366941295.png

 

Thanks in Advance!

0 Likes
Message 4 of 13

WCrihfield
Mentor
Mentor

Just another little tip...you may also want to ensure that the current component (the one within the loop & selected) is actually 'Visible = True' within the loop somewhere, just to be thorough.  I know that manually, when you select a component that has already had its visibility turned off, the Isolate command is not shown, and the Undo Isolate command is greyed out, so I don't know if that would cause an error when you execute the Isolate command on a selected component that was already not visible itself or not.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 13

dalton98
Collaborator
Collaborator
Accepted solution

Try This. If you just want 1 component per representation comment out the red text

Dim oAssDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oReps As DesignViewRepresentations
oReps = oAssDoc.ComponentDefinition.RepresentationsManager.DesignViewRepresentations
Dim oOcc As ComponentOccurrence

'blank representation
oReps.Add("dummy")
oReps.Item("dummy").Activate
For Each oOcc In oAssDoc.ComponentDefinition.Occurrences
	If oOcc.Suppressed = True Then Continue For
	oOcc.Visible = False
Next

For Each oOcc In oAssDoc.ComponentDefinition.Occurrences
	oReps.Item("dummy").Activate
	Dim oOccName As String = oOcc.ReferencedDocumentDescriptor.DisplayName
	If oOcc.Suppressed = True Then Continue For
	Try
	oReps.Item(oOccName).Activate
	Catch
	oReps.Add(oOccName)
	oReps.Item(oOccName).Activate
'	oOcc.Visible = True
	End Try
	
	For Each oOcc1 In oAssDoc.ComponentDefinition.Occurrences
		If oOcc1.Suppressed = True Then Continue For
		If oOcc1.ReferencedDocumentDescriptor.DisplayName = oOccName
			oOcc1.Visible = True
		End If
	Next
Next
	
oReps.Item("dummy").Delete
Message 6 of 13

get2dpk
Participant
Participant

hi @dalton98 

Thanks for the Solution. It works perfectly

Have a good day!😄

0 Likes
Message 7 of 13

get2dpk
Participant
Participant

Hi @dalton98 @WCrihfield 

 

Hope you  guys are doing good.

Its been a long back since I got the Solution.

I have been using this Code and it works great.

Still  I am facing an minor problem. So I am writing it to get your answer whether it can be solved or not.

lets say I have 10 unique IPTs in a iam. I run the code and it creates 10 corresponding  View Representation.

After that for  requirement, If I add a 11th Ipt into the assy , then those 10 View representation (Already created) has the newly added IPT inside them.  So I have to go to each View representation ,Isolate the  specific Parts once again , then save it.(10 times). 
To Avoid this , no matter how many ipts are added in later state, Is it possible  to modify code so that user can run the code again to retain the True Isolation Views & to create the new View representations for the Later Added IPT. (11th one in this case)?. So user can run the code again and again to update the View isolation.

 

0 Likes
Message 8 of 13

WCrihfield
Mentor
Mentor

Hi @get2dpk.  That behavior has been a pain for a long time.  The DVR (DesignViewRepresentation) does have the Locked property, but that does not exactly seem to fit this need.  It just prevents changes to that DVR from being saved in that DVR when you save the document.  It would be nice to have an additional property for another type of lock on that DVR, which would prevent any other components from showing up in it.  But that may not be feasible for the folks at Autodesk to provide a solution for, and enforce.

 

Below is another similar iLogic rule example that you should be able to run as many times as you want to keep a DVR for each component, and keep that component the only one visible in that DVR.  I chose to go the loop route, instead of the Try...Catch statement for ensuring the needed DVR exists, simply because we can, and it is the proper way (even if I do not follow my own advise all the time 😉).  It gets all collections first, then starts iterating the components.  If a component is suppressed, we can not do much with it, without it throwing errors, so we check for that status first, and skip over those.  (If we need to deal with those too, then we may need to add in some extra code for handling that.)  Then we try to find an existing DVR with the same name as the component, by iterating the existing DVR's and checking their name against the name of the current component.  If it was not found, it will get created.  Then that DVR is activated, all components hidden, and that one component's visibility turned on.  Then we activate the originally active DVR, to avoid disruption of what you were doing before.  Then update the assembly, if needed.  Give this a try, and see if it does what you need it to.  And if needed, we may want to use a helper event handler to automate this rule to run when certain events happen.

 

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then Return
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	Dim oOccs As ComponentOccurrences = oADef.Occurrences
	Dim oRepsMgr As RepresentationsManager = oADef.RepresentationsManager
	Dim oDVRs As DesignViewRepresentations = oRepsMgr.DesignViewRepresentations
	Dim oOrigDVR As DesignViewRepresentation = oRepsMgr.ActiveDesignViewRepresentation
	For Each oOcc As ComponentOccurrence In oOccs
		If oOcc.Suppressed Then Continue For
		Dim oOccDVR As DesignViewRepresentation = Nothing
		For Each oDVR As DesignViewRepresentation In oDVRs
			If oDVR.Name = oOcc.Name Then
				oOccDVR = oDVR
				Exit For
			End If
		Next 'oDVR
		If oOccDVR Is Nothing Then
			oOccDVR = oDVRs.Add(oOcc.Name)
		End If
		oOccDVR.Activate
		oOccDVR.HideAll
		oOcc.Visible = True
	Next 'oOcc
	oOrigDVR.Activate 'restore originally active DVR
	If oADoc.RequiresUpdate Then oADoc.Update2(True)
	'If oADoc.Dirty Then oADoc.Save2(True)
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 9 of 13

J-Camper
Advisor
Advisor
Accepted solution

@get2dpk,

 

You will have to adjust @WCrihfield 's code to match the original solution by

  • name the view rep as document DisplayName
  • add the loop to show all occurrences with the same documentdescriptor in the view rep [if wanted]

I modified the original solution if you want that:

Dim aDoc As AssemblyDocument = TryCast(ThisApplication.ActiveDocument, AssemblyDocument)
If IsNothing(aDoc) Then Logger.Debug("Not Run In Assemnly Document") : Exit Sub

Dim oReps As DesignViewRepresentations = aDoc.ComponentDefinition.RepresentationsManager.DesignViewRepresentations

'Current Representation
Dim currentRep As DesignViewRepresentation = aDoc.ComponentDefinition.RepresentationsManager.ActiveDesignViewRepresentation

For Each oOcc As ComponentOccurrence In aDoc.ComponentDefinition.Occurrences
	If oOcc.Suppressed = True Then Continue For

	Dim oOccName As String = oOcc.ReferencedDocumentDescriptor.DisplayName
	Try
		oReps.Item(oOccName).Activate()
	Catch
		oReps.Add(oOccName)
	End Try
	oReps.Item(oOccName).HideAll()
	oOcc.Visible = True
	
	For Each oOcc1 In aDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oOcc.ReferencedDocumentDescriptor)
		If oOcc1.Suppressed = True Then Continue For
		oOcc1.Visible = True
	Next
	
Next
	
currentRep.Activate()

 

Let me know if you have any questions, or issues

Message 10 of 13

J-Camper
Advisor
Advisor
Accepted solution

I actually changed it up a bit more to avoid looping occurrences twice:

 

Dim aDoc As AssemblyDocument = TryCast(ThisApplication.ActiveDocument, AssemblyDocument)
If IsNothing(aDoc) Then Logger.Debug("Not Run In Assemnly Document") : Exit Sub

Dim oRepsManager As RepresentationsManager = aDoc.ComponentDefinition.RepresentationsManager
'Current Representation
Dim currentRep As DesignViewRepresentation = oRepsManager.ActiveDesignViewRepresentation
If currentRep.DesignViewType = DesignViewTypeEnum.kTransientDesignViewType Then currentRep = Nothing 'avoids weird error when returning to "[Primary]" after adding view reps

For Each RefDocDisc As DocumentDescriptor In aDoc.ReferencedDocumentDescriptors
	Dim DuplicateComponentCollection As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
	For Each ItemComp As ComponentOccurrence In aDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(RefDocDisc)
		If ItemComp.Suppressed = True Then Continue For
		DuplicateComponentCollection.Add(ItemComp)
'		Exit For     'Uncomment if you only want the first unsuppressed component in the view
	Next
	If DuplicateComponentCollection.Count < 1 Then Continue For
	
	Dim NewRep As DesignViewRepresentation
	Try
		NewRep = oRepsManager.DesignViewRepresentations.Item(RefDocDisc.DisplayName)
	Catch
		NewRep = oRepsManager.DesignViewRepresentations.Add(RefDocDisc.DisplayName)
	End Try
	NewRep.Activate()
	NewRep.HideAll()
	NewRep.SetVisibilityOfOccurrences(DuplicateComponentCollection, True)
Next

If Not IsNothing(currentRep) Then currentRep.Activate() 'avoids weird error when returning to "[Primary]" after adding view reps

 

Message 11 of 13

WCrihfield
Mentor
Mentor
Accepted solution

I guess I did not pay good enough attention to the previous responses where it was mentioned that you needed all components referencing the same model file (resulting in same base component name) to be visible in each view rep.  I had that similar sounding code on hand, so I posted what I had, without looking into it deep enough.  With that added functionality in mind, and with a couple of the tips mentioned by @J-Camper in mind, I modified a copy of my earlier code.  I kept my DVR's loop in place, instead of the Try...Catch statement, for finding/creating the needed DVRs, but did the other loops and collections a bit differently, blending some existing and new stuff together.  Just as another interesting example to look at for the same basic task.  Not that there is anything wrong with the previous versions, just playing around with it a bit.  

The code is in the attached text file.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 13

WCrihfield
Mentor
Mentor
Accepted solution

Also just a couple more tips to keep in mind, if performance is an important factor in your code solutions...

 

Using 'Is Nothing' is generally preferred over using 'IsNothing()', because IsNothing() sends the input object to another vb.net Function, then receives the result back from that other Function, which takes a little more processing than the 'Is Nothing' way, because it just directly evaluates it in place, with no external calls.  I used to use IsNothing a lot myself also, until I read multiple discussions about it online, then went back and changed that little detail in a ton of my work related rules.

 

Also, when you are planning on iterating through a collection, it is best to get the collection to a variable before the start of the loop, then reference that variable.  This is because, if you are 'getting' the collection within the definition line of the loop, it is re-obtaining that collection each time it iterates, reducing performance.  At leas that is what I have heard, which is why I usually get my collections to a variable before my loops start in all my latest examples, and in my work related code solutions.  Another one I picked up not too long ago, and used to do differently myself.

 

I have also read multiple discussions about, if an Try...Catch statement 'can be avoided' by using a simple loop with If...Then statement, then which is more efficient/proper.  Well avoiding the Try...Catch statement, when you can, seems to be the 'proper' way.  Whether or not it is more efficient (as far as processing) is more hotly debated and there are several variables involved, but most agree that the regular loop with If...Then statement is more efficient (again in processing).  Using a Try...Catch statement is often much faster to type though because it usually requires less code when we are in a hurry.  My own codes are still mixed with both, but I am trying to revert most to using loops where possible.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 13 of 13

get2dpk
Participant
Participant

 Hi @WCrihfield @J-Camper 

Sorry for Late reply, I was not able to  try your solution for the past 5 days.

I tried it  now and it works perfectly.

Thanks a Lot 🤝🏼 to both of you. 

Really appreciate your time and effort!😀👍🏼👍🏼

Have a Good day! 
I will mark this one as Accept solution.

0 Likes