ILogic is it Possible to Make Contraints.AddMate Reference the active document instead of the document the rule is running in?

ILogic is it Possible to Make Contraints.AddMate Reference the active document instead of the document the rule is running in?

werft60
Contributor Contributor
1,003 Views
7 Replies
Message 1 of 8

ILogic is it Possible to Make Contraints.AddMate Reference the active document instead of the document the rule is running in?

werft60
Contributor
Contributor

Hello,

 

Apologies if I posted this in the wrong place this is my first post. 

 

Basically I've made a rule that takes in some user information and then copies an existing assembly and places it in a new file location. The rule then opens this new assembly file and makes some edits. The edits are working perfectly until I try to add some mates to the assembly. 

 

I'm currently using: 

Constraints.AddMate("Door_YZ" & MateNumber, {DoorName, "Stile:1" }, YZFace, "", "YZ Plane", XOffset)

Though I'm getting an error because the rule doesn't recognize the component name (DoorName, "Stile:1"). This is because Constraints.AddMate is referenced by ThisRule which is the document where the rule is running not the one I'm trying to edit. 

 

Is there any other way to add constraints where I can reference the active file?

 

Thanks

 

 

0 Likes
Accepted solutions (3)
1,004 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Yes. That iLogic snippet is basically a shortcut tool, but it definitely has a standard counterpart.  You will need to drill down through the object model to get to the related methods though.  Under the AssemblyDocument, then the AssemblyComponentDefinition, then the AssemblyConstraints, is a list of similar methods for creating constraints , one of which is called 'AddMateConstraint()' (or its later version 'AddMateConstraint2(). 

 

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8

werft60
Contributor
Contributor

Thanks for the detailed response!

 

I have it mostly figured out but I'm struggling to figure out how to get a named face from a component as an object.

 

With the Constraints.AddMate I could just specify: 

 

Constraints.AddMate("Door_YZ" & MateNumber, {DoorName, "Stile:1" }, YZFace, "", "YZ Plane", XOffset)

 Which is just the name of a sub assembly and then the specific part of the subassembly and then the named face on that part. 

 

However with AddMateConstraint it just wants an object as an input so I'm guessing I have to get that face as an object beforehand and then feed it in but I'm not quite sure how to do that. 

 

Also because this is a subassembly in an assembly am I going to have to use some sort of proxy?

 

Thanks again!

 

 

0 Likes
Message 4 of 8

WCrihfield
Mentor
Mentor

I honestly haven't gone that extra step deep in an assembly when creating constraints between named faces before, so you may have to test the code out yourself to make sure it will work for you.  I believe you have to create the Proxy type object from a top level component's CreateGeometryProxy method for any objects that are at that level or below, so that the Proxy object will be available in to the main assembly.

You can try this:  (you will need to change the variable values near the top, or just eliminate them if pasting into your existing larger code)

 

'code to replace the following:
'Constraints.AddMate("Door_YZ" & MateNumber, {DoorName, "Stile:1" }, YZFace, "", "YZ Plane", XOffset)

'these seem to be variables, because they aren't in quotes in your example line of code
Dim DoorName As String = "My Door:1"
Dim YZFace As String = "YZFace"
Dim XOffset As Double = 0

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition

'get the FaceProxy of the NamedFace in the sub-component
Dim oSubAsm As ComponentOccurrence = oADef.Occurrences.ItemByName(DoorName)
Dim oSubComp As ComponentOccurrence = oSubAsm.Definition.Occurrences.ItemByName("Stile:1")
Dim oNE As NamedEntities = iLogicVb.Automation.GetNamedEntities(oSubComp.Definition.Document)
Dim oNamedFace As Face = oNE.TryGetEntity(YZFace)
Dim oNFaceProxy As FaceProxy
oSubAsm.CreateGeometryProxy(oNamedFace, oNFaceProxy)

Dim oYZPlane As WorkPlane = oADef.WorkPlanes.Item("YZ Plane")

Dim oConst As MateConstraint = oADef.Constraints.AddMateConstraint(oNFaceProxy, oYZPlane, XOffset)

 

 

Here's a tested and working example iLogic code that creates a mate constraint between named faces in two top level part type components.

 

Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition

Dim oComp1 As ComponentOccurrence = oADef.Occurrences.ItemByName("TestPart1:1")
Dim oC1NEs As NamedEntities = iLogicVb.Automation.GetNamedEntities(oComp1.Definition.Document)
Dim oC1NFace As Face = oC1NEs.TryGetEntity("Top Face")
Dim oC1NFaceProxy As FaceProxy
oComp1.CreateGeometryProxy(oC1NFace, oC1NFaceProxy)

Dim oComp2 As ComponentOccurrence = oADef.Occurrences.ItemByName("Part3:1")
Dim oC2NEs As NamedEntities = iLogicVb.Automation.GetNamedEntities(oComp2.Definition.Document)
Dim oC2NFace As Face = oC2NEs.TryGetEntity("Bottom Face")
Dim oC2NFaceProxy As FaceProxy
oComp2.CreateGeometryProxy(oC2NFace, oC2NFaceProxy)

Dim oConst As MateConstraint = oADef.Constraints.AddMateConstraint(oC1NFaceProxy, oC2NFaceProxy, 0)

 

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 8

werft60
Contributor
Contributor

I'm at a loss with this one, I'm getting a "The parameter is incorrect.". I checked the oNamedFace variable and it doesn't come back as Nothing so it should contain the right face object. I'm not sure how the proxy could be wrong either. 

Dim oADef As AssemblyComponentDefinition = oAssemblyDoc.ComponentDefinition
Dim oSubAsm As ComponentOccurrence = oADef.Occurrences.ItemByName(DoorName)
Dim oSubComp As ComponentOccurrence = oSubAsm.Definition.Occurrences.ItemByName("Stile:1")
Dim oNE As NamedEntities = iLogicVb.Automation.GetNamedEntities(oSubComp.Definition.Document)
Dim oNamedFace = oNE.TryGetEntity(YZFace)
Dim oNFaceProxy As FaceProxy
'Error Happens Here
oSubAsm.CreateGeometryProxy(oNamedFace, oNFaceProxy)

 

Alternatively is it possible to insert a rule into a document? That way I could separate this batch of code into its own rule and run it within the new assembly document. This way I could just use the original code I was using and it would reference the right data.

 

Or is there anyway to run an external rule on a specific document? I know for internal rules you have to specify a document but with external rules they don't.

 

 

 

0 Likes
Message 6 of 8

WCrihfield
Mentor
Mentor
Accepted solution

Again...are you defining those unquoted variables (DoorName, YZFace) somewhere, or are they the names of local parameters?

Just in case I'm not thinking clearly, and the part where you create the geometry proxy doesn't need to be done from a top level component...

Try changing this:

oSubAsm.CreateGeometryProxy(oNamedFace, oNFaceProxy)

to this:

oSubComp.CreateGeometryProxy(oNamedFace, oNFaceProxy)

 and see if that helps.

Yes, you can insert a rule into a document.  There are a couple ways to do this.  One is to copy it from one document to another.  Another way is to create the rule from a text file or have the text for it directly within the rule that is injecting it into the other document.  Here is a link to one of my contribution posts that shows you how to create a rule and inject it into a document.  There are also other similar rules for copying iLogic rules from one document to another within my contribution posts, and elsewhere here on the forums.

 

As for running an external rule on a specific document, yes, it is also possible.  Look under 'iLogicVb.Automation.RunExternalRule().

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 8

werft60
Contributor
Contributor
Accepted solution

The batch of code I'm working in is its own separate sub, so the DoorName and YZFace variables are defined elsewhere and then passed to it in the arguments. 

 

I tried what you suggested and it worked however I just get an unspecified error when I try to add the mate.

 

What did work was just injecting the rule into the assembly file after I copy it! Seems like this solved all the reference issues and allows me to just revert to my original code. 

 

There is also a simple way to cheat it if you don't have too many template files (my code copies a base template to start from). If you only have a couple of template files then you can just paste the rule into the originals and it will get copied with the whole document and you can just run it as an internal rule. Only downfall to this is that if the rule needs to be updated for any reason you have to update it at each template file which could tedious and tricky if you have more then a couple. 

 

Best solution is to insert the rule after the base file is copied.

 

Thanks again for all your help I really appreciate it.

0 Likes
Message 8 of 8

WCrihfield
Mentor
Mentor
Accepted solution

If the local code is going to be exactly the same in all your documents being produced from the template, why not make the rule an external rule, then use a small simple local rule that just runs that external rule.  This simple rule would then be the one located in your template, and the rule that would get copied to all new documents, based on that template.  This way, the main complex part of the code is in one place, easily fixable, when needed.  Or maybe I misunderstood and that is already your plan.  I use a lot of those simple 'run external rules' type of local rules, to avoid the duplication of complex code.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes