Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Remove Constraints and ground components within subassembly

1 REPLY 1
Reply
Message 1 of 2
melle.oosterman
961 Views, 1 Reply

Remove Constraints and ground components within subassembly

I am trying to remove all constraints, and ground all components within a subassembly.

 

I am replacing components within a subassembly. But to make this easy, and without iMates, I have to remove all constraints, and ground all components within that subassembly.

 

I tried these two codes:

 

SyntaxEditor Code Snippet

'start of ilogic code' get the active assembly
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'set the Master LOD active
Dim oLODRep As LevelofDetailRepresentation
oLODRep = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("Master")
oLODRep.Activate

 'Iterate through all of the occurrences and ground them.
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
'check for and skip virtual components
If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
oOccurrence.Grounded = True
Else
End If

 

 

SyntaxEditor Code Snippet

Dim oAssDoc As AssemblyDocument
oAssDoc = ThisApplication.ActiveDocument
Dim oConstraint As AssemblyConstraint

  i = 0
          For Each oConstraint In oAssDoc.ComponentDefinition.Constraints
            
          oConstraint.Delete
            i = i + 1
          
          Next
MessageBox.Show(" A total of "&  i & " constraints were deleted.", "iLogic")

 

 

These work perfect in an assembly, but they do nothing to subassemblies. 

 

I even tried to put them inside the subassembly and running it within the subassembly by using an external rule. But this also doesn't work. 

If I try this I will get these errors:

 

Constraints;

It doesn't give an error, but it just doesn't remove any constraints.

I will get the message: A total of 0 constraints were deleted. 

 

Grounden; 

Error in rule: Grounden, in document: subassembly.iam

The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

 

If I open the subassembly, and run the rules there everything works fine again. 

 

I hope someone can help me out!

 

 

Melle Oosterman

Inventor Autodesk Inventor 2014

1 REPLY 1
Message 2 of 2
CadUser46
in reply to: melle.oosterman

Melle.  I cant reproduce the code required without doing the work needed but the process would be something like this.

 

I would move the constraint deleting and grounding to its own sub where i would pass in the assembly you want it applied to.

 

For each component occurence you need to check if typeof is kassemblycomponentdefinition then pass this into the sub.  This could be a bit complicated so another way might be this;

 

Instead of using the bom you should navigate something like

 

Dim odoc as inventor.assemblydocument: set odoc = thisapplication.activedocument.

Dim oChildDoc as inventor.document.

 

For each oChildDoc in odoc.allreferencedfiles

   if ochildDoc.componentdefinition.type = assembly then

     pass to sub (ochilddoc)

  end if

next

 

Then at the end pass in odoc.

 

Also you might need to add in a check for the windows file state 'ReadOnly' and change it before editing.  Assuming you are allowed to edit all these files.

 

PPS i would also do it in VBA as its much easier to bug test/step through.  And you can navigate the API.

 


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report