Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assembly Constraints using NX Open 2

Status
Not open for further replies.

biw01

Automotive
Dec 31, 2011
152
I am new to NX open programming.
I have written a program in VB.NET to assemble components using
assembly constraints.
However when i try to run the program , even though all the assembly
constraints gets created successfully and their constraint solver
status as "Solved", Still the parts are distanced apart and they are
not mating.
Any Idea on what i am missing in my code?

I have developed the code mainly by recording journal in NX6.0
 
Replies continue below

Recommended for you

I haven't done much work automating constraints, but I'd be willing to take a look if you post your code. If you have a small simplified assembly that you are testing with you should provide that as well, or at least instructions on how to recreate it.
 
The code is below.

Begin of Assembly Creation

Public Sub SCreateAssemlyConstraints(ByVal workPart As Part)
Dim objComponentPositioner As Positioning.ComponentPositioner
Dim objarrangement As Arrangement
Dim objNetwork As Positioning.Network

_objComponentPositioner = workPart.ComponentAssembly.Positioner
_objComponentPositioner.ClearNetwork()

objarrangement = CType(workPart.ComponentAssembly.Arrangements.FindObject("Arrangement 1"), Arrangement)
_objComponentPositioner.PrimaryArrangement = objarrangement
_objComponentPositioner.BeginAssemblyConstraints()

objNetwork = _objComponentPositioner.EstablishNetwork()

_objComponentNetwork = CType(objNetwork, Positioning.ComponentNetwork)
_objComponentNetwork.MoveObjectsState = True
Dim nullAssemblies_Component As Assemblies.Component = Nothing
_objComponentNetwork.DisplayComponent = nullAssemblies_Component
_objComponentNetwork.MoveObjectsState = True
_objComponentNetwork.NetworkArrangementsMode = Positioning.ComponentNetwork.ArrangementsMode.Existing

End Sub


Creating Constraints
Public Sub CreateConstraint(ByVal sTypeofConstraint As String, ByVal objComp1ToMate As NXObject, ByVal objComp2ToMate As NXObject, _
ByVal objComponent1 As NXObject, ByVal objComponent2 As NXObject)
Dim objConstraint As Positioning.Constraint
'Dim objComp1 As Object
'Dim objComp2 As Object
Dim objComponentConstraint As Positioning.ComponentConstraint
Dim objConstraintReference1 As Positioning.ConstraintReference
Dim objConstraintReference2 As Positioning.ConstraintReference

objConstraint = _objComponentPositioner.CreateConstraint()
objComponentConstraint = CType(objConstraint, Positioning.ComponentConstraint)

_objComponentNetwork.AddConstraint(objConstraint)
If sTypeofConstraint = "Touch&Align-Axis" Then
'objComp1 = CType(objComponent1, DatumAxis)
'objComp2 = CType(objComponent1, DatumAxis)
objComponentConstraint.ConstraintAlignment = Positioning.Constraint.Alignment.CoAlign
objComponentConstraint.ConstraintType = Positioning.Constraint.Type.Touch
objConstraintReference1 = objComponentConstraint.CreateConstraintReference(objComp1ToMate, objComponent1, False, False, False)
objConstraintReference2 = objComponentConstraint.CreateConstraintReference(objComp2ToMate, objComponent2, False, False, False)
objConstraintReference1.SetFixHint(True)
objConstraintReference1.SetFixHintForUpdate(True)
objConstraintReference2.SetFixHint(True)
objConstraintReference2.SetFixHintForUpdate(True)

ElseIf sTypeofConstraint = "Touch&Align-Face" Then
'objComp1 = CType(objComponent1, Face)
'objComp2 = CType(objComponent1, Face)
objComponentConstraint.ConstraintAlignment = Positioning.Constraint.Alignment.InferAlign 'Positioning.Constraint.Alignment.InferAlign
objComponentConstraint.ConstraintType = Positioning.Constraint.Type.Touch
objConstraintReference1 = objComponentConstraint.CreateConstraintReference(objComp1ToMate, objComponent1, False, False, False)
objConstraintReference2 = objComponentConstraint.CreateConstraintReference(objComp2ToMate, objComponent2, False, False, False)
objConstraintReference1.SetFixHint(True)
objConstraintReference1.SetFixHintForUpdate(True)
objConstraintReference2.SetFixHint(True)
objConstraintReference2.SetFixHintForUpdate(True)
objComponentConstraint.SetAlignmentHint(Positioning.Constraint.Alignment.ContraAlign)

ElseIf sTypeofConstraint = "Parallel" Then
'objComp1 = CType(objComponent1, Face)
'objComp2 = CType(objComponent1, Face)
objComponentConstraint.ConstraintType = Positioning.Constraint.Type.Parallel
objConstraintReference1 = objComponentConstraint.CreateConstraintReference(objComp1ToMate, objComponent1, False, False, False)
objConstraintReference2 = objComponentConstraint.CreateConstraintReference(objComp2ToMate, objComponent2, False, False, False)
objConstraintReference1.SetFixHint(True)
objConstraintReference1.SetFixHintForUpdate(True)
objConstraintReference2.SetFixHint(True)
objConstraintReference2.SetFixHintForUpdate(True)
End If
objConstraint.ReverseDirection()

'objComponentConstraint.FlipAlignment()
_objComponentNetwork.Solve()

_objComponentNetwork.ApplyToModel()
objConstraint = Nothing
objComponentConstraint = Nothing
objConstraintReference1 = Nothing
objConstraintReference2 = Nothing

End Sub


End Assembly Constraints
Public Sub SEndAssemblyConstraints()
_objComponentNetwork.ResetDisplay()
_objComponentNetwork.ApplyToModel()
_objComponentPositioner.ClearNetwork()
Dim nErrs1 As Integer
nErrs1 = FnGetNxSession().UpdateManager.AddToDeleteList(_objComponentNetwork)
_objComponentPositioner.DeleteNonPersistentConstraints()
Dim nullAssemblies_Arrangement As Assemblies.Arrangement = Nothing
_objComponentPositioner.PrimaryArrangement = nullAssemblies_Arrangement
_objComponentPositioner.EndAssemblyConstraints()

End Sub

 
 http://files.engineering.com/getfile.aspx?folder=e58f6e24-097e-4623-aaae-64fa6a4341dd&file=My_Assembly.jpg
I have also noticed that even though the solver status is solved for all the assembly constraints , yet when i click redefine in the dialog that opens , it only shows beside geometry in brackets (1) instead of 2
Although the connected features are getting highlighted in the created assembly.

Redefine Dialog

Constraint Info

Parallel Constraint Created
Your file's link is:
 
From the parallel_constraint.jpg that you posted of the assembly, it appears that the faces are indeed parallel (difficult to tell for sure from that angle, but looks like they are). If the faces started off as parallel before you created the constraint, the components will not move. Try deleting that constraint, rotating one of the components and running the journal again to see if the component moves. It is odd that the redefine dialog lists 1 object, but both are highlighted.

Nothing in your code really stands out to me, I would suggest adding some code to output to the listing window and echoing the values of variables/objects along the way to get a better idea of what is really happening.

 
On digging further , i found in the help file a property called
NxOpen.Preferences.SessionAssemblies.InterPartPositioning

However i am unable to use the property in my NX 6.0 code.
Any idea what is the alternative in NX 6.0 NX Open Code.

The Help states "Returns or sets the option to allow interpart geometry selection in the assembly constraints dialog " for the above property.
 
"If the faces started off as parallel before you created the constraint, the components will not move" , If that is the case how do i mate the two components and how do i check whether the faces are parallel before the constraint.
My end result should be irrespective of the current positioning , the components should get mated properly with respect to the constraints.
Also i am unable to understand why does the redefine dialog show geometry selected as 1 when in the assembly it highlights both the geometries.
 
Perhaps I should have worded that differently; if the faces are parallel before you create a parallel constraint, the components will not move on screen - not because there is something keeping them from moving, but simply because they don't need to to satisfy the constraint. The parts are already in position according to that one constraint. If you need the clamp arm to move down toward the clamp, you'll need to add one or more additional constraints to accomplish that.

Also i am unable to understand why does the redefine dialog show geometry selected as 1 when in the assembly it highlights both the geometries.
This is indeed the puzzling bit. Since I don't have access to your code and parts, I recorded a journal while applying constraints - the code generated looked very similar to the parts of your code that you posted, but the redefine dialog worked correctly for me.

 
that sounds good.Can you please share your recorded journal code in which the redefine dialog works correctly for you.
Also should i try using the Fix constraint on the clamp part and then constrain the other 2 parts with respect to these two parts.
 
I have also noticed a strange thing even though i am trying to use CoAlign Alignment type in my code to align the axis.When i turn on the journal recording and then run my VB.NET code , the journal recorded shows ContraAlign instead of CoAlign for the axis

My code[\b]
If sTypeofConstraint = "Touch&Align-Axis" Then
'objComp1 = CType(objComponent1, DatumAxis)
'objComp2 = CType(objComponent1, DatumAxis)
objComponentConstraint.ConstraintAlignment = Positioning.Constraint.Alignment.CoAlign
objComponentConstraint.ConstraintType = Positioning.Constraint.Type.Touch
objConstraintReference1 = objComponentConstraint.CreateConstraintReference(objComp1ToMate, objComponent1, False, False, False)
objConstraintReference2 = objComponentConstraint.CreateConstraintReference(objComp2ToMate, objComponent2, False, False, False)
objConstraintReference1.SetFixHint(True)
objConstraintReference1.SetFixHintForUpdate(True)

Journal Recorded Code obtained running my programme[\b]
Dim componentConstraint1 As Positioning.ComponentConstraint = CType(constraint1, Positioning.ComponentConstraint)

componentConstraint1.ConstraintAlignment = Positioning.Constraint.Alignment.InferAlign

componentConstraint1.ConstraintType = Positioning.Constraint.Type.Touch

Dim constraintReference1 As Positioning.ConstraintReference
constraintReference1 = componentConstraint1.CreateConstraintReference(component2, datumAxis1, False, False, False)

Dim constraintReference2 As Positioning.ConstraintReference
constraintReference2 = componentConstraint1.CreateConstraintReference(component4, datumAxis2, False, False, False)

constraintReference1.SetFixHint(True)

componentNetwork1.Solve()
 
Below is the journal I recorded. My test assembly was 2 simple 'blocks' as components, and I used a touch constraint on 2 of the faces. After I recorded the journal, I deleted the constraint, ran the journal and checked the redefine dialog. It correctly highlighted the 2 faces and reported (2) objects as being used.

Code:
' NX 7.5.0.32
'
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
'   Menu: Assemblies->Component Position->Assembly Constraints...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim componentPositioner1 As Positioning.ComponentPositioner
componentPositioner1 = workPart.ComponentAssembly.Positioner

componentPositioner1.ClearNetwork()

Dim arrangement1 As Assemblies.Arrangement = CType(workPart.ComponentAssembly.Arrangements.FindObject("Arrangement 1"), Assemblies.Arrangement)
componentPositioner1.PrimaryArrangement = arrangement1

componentPositioner1.BeginAssemblyConstraints()

Dim allowInterpartPositioning1 As Boolean
allowInterpartPositioning1 = theSession.Preferences.Assemblies.InterpartPositioning

Dim network1 As Positioning.Network
network1 = componentPositioner1.EstablishNetwork()

Dim componentNetwork1 As Positioning.ComponentNetwork = CType(network1, Positioning.ComponentNetwork)

componentNetwork1.MoveObjectsState = True

Dim nullAssemblies_Component As Assemblies.Component = Nothing

componentNetwork1.DisplayComponent = nullAssemblies_Component

componentNetwork1.NetworkArrangementsMode = Positioning.ComponentNetwork.ArrangementsMode.Existing

theSession.SetUndoMarkName(markId1, "Assembly Constraints Dialog")

componentNetwork1.MoveObjectsState = True

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Assembly Constraints Update")

Dim rotMatrix1 As Matrix3x3
rotMatrix1.Xx = 0.900483351968208
rotMatrix1.Xy = 0.0335721640585612
rotMatrix1.Xz = 0.433592715146975
rotMatrix1.Yx = -0.275765289860863
rotMatrix1.Yy = 0.815020565584467
rotMatrix1.Yz = 0.509602769402139
rotMatrix1.Zx = -0.336278512153327
rotMatrix1.Zy = -0.578458630737584
rotMatrix1.Zz = 0.743170488373391
Dim translation1 As Point3d = New Point3d(-15.2091219822707, -2.50274212963751, 8.08264700947146)
workPart.ModelingViews.WorkView.SetRotationTranslationScale(rotMatrix1, translation1, 0.243863736713582)

Dim rotMatrix2 As Matrix3x3
rotMatrix2.Xx = 0.818062547608542
rotMatrix2.Xy = 0.574101389456565
rotMatrix2.Xz = 0.0343695042772528
rotMatrix2.Yx = -0.277416222689856
rotMatrix2.Yy = 0.341542983125992
rotMatrix2.Yz = 0.89799144208945
rotMatrix2.Zx = 0.503799471604243
rotMatrix2.Zy = -0.744147824898682
rotMatrix2.Zz = 0.438668561797908
Dim translation2 As Point3d = New Point3d(-14.4195735017611, -3.89000909896482, -0.917898482618078)
workPart.ModelingViews.WorkView.SetRotationTranslationScale(rotMatrix2, translation2, 0.238183791552755)

Dim markId3 As Session.UndoMarkId
markId3 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Create Constraint")

Dim constraint1 As Positioning.Constraint
constraint1 = componentPositioner1.CreateConstraint()

Dim componentConstraint1 As Positioning.ComponentConstraint = CType(constraint1, Positioning.ComponentConstraint)

componentConstraint1.ConstraintAlignment = Positioning.Constraint.Alignment.InferAlign

componentConstraint1.Persistent = True

componentConstraint1.ConstraintType = Positioning.Constraint.Type.Touch

Dim component1 As Assemblies.Component = CType(workPart.ComponentAssembly.RootComponent.FindObject("COMPONENT test_comp2 1"), Assemblies.Component)

Dim face1 As Face = CType(component1.FindObject("PROTO#.Features|BLOCK(0)|FACE 3 {(0,4,4) BLOCK(0)}"), Face)

Dim constraintReference1 As Positioning.ConstraintReference
constraintReference1 = componentConstraint1.CreateConstraintReference(component1, face1, False, False, False)

Dim helpPoint1 As Point3d = New Point3d(20.5, 4.00578507010411, 5.78865875843652)
constraintReference1.HelpPoint = helpPoint1

Dim component2 As Assemblies.Component = CType(workPart.ComponentAssembly.RootComponent.FindObject("COMPONENT test_comp1 1"), Assemblies.Component)

Dim face2 As Face = CType(component2.FindObject("PROTO#.Features|BLOCK(0)|FACE 6 {(10,5,5) BLOCK(0)}"), Face)

Dim constraintReference2 As Positioning.ConstraintReference
constraintReference2 = componentConstraint1.CreateConstraintReference(component2, face2, False, False, False)

Dim helpPoint2 As Point3d = New Point3d(10.0, 8.15214612309787, 6.53388392507978)
constraintReference2.HelpPoint = helpPoint2

constraintReference2.SetFixHint(True)

componentConstraint1.SetAlignmentHint(Positioning.Constraint.Alignment.ContraAlign)

componentNetwork1.Solve()

Dim rotMatrix3 As Matrix3x3
rotMatrix3.Xx = 0.84137975223596
rotMatrix3.Xy = 0.538967539341624
rotMatrix3.Xz = -0.0399262327723193
rotMatrix3.Yx = -0.236234875315951
rotMatrix3.Yy = 0.433217631810186
rotMatrix3.Yz = 0.869779033532788
rotMatrix3.Zx = 0.486079413482829
rotMatrix3.Zy = -0.722382499113046
rotMatrix3.Zz = 0.491823473172417
Dim translation3 As Point3d = New Point3d(-13.8211811519369, -4.69878410763003, -1.13712218839268)
workPart.ModelingViews.WorkView.SetRotationTranslationScale(rotMatrix3, translation3, 0.240464719615703)

Dim markId4 As Session.UndoMarkId
markId4 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Assembly Constraints")

componentNetwork1.Solve()

componentNetwork1.ResetDisplay()

componentNetwork1.ApplyToModel()

componentPositioner1.ClearNetwork()

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(componentNetwork1)

Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId2)

componentPositioner1.DeleteNonPersistentConstraints()

Dim nErrs3 As Integer
nErrs3 = theSession.UpdateManager.DoUpdate(markId2)

theSession.DeleteUndoMark(markId4, Nothing)

theSession.SetUndoMarkName(markId1, "Assembly Constraints")

Dim nullAssemblies_Arrangement As Assemblies.Arrangement = Nothing

componentPositioner1.PrimaryArrangement = nullAssemblies_Arrangement

componentPositioner1.EndAssemblyConstraints()

theSession.DeleteUndoMark(markId2, Nothing)

theSession.DeleteUndoMark(markId3, Nothing)

' ----------------------------------------------
'   Menu: Tools->Journal->Stop Recording
' ----------------------------------------------
Dim rotMatrix4 As Matrix3x3
rotMatrix4.Xx = 0.839789139499119
rotMatrix4.Xy = 0.541434295380841
rotMatrix4.Xz = -0.0400387932483081
rotMatrix4.Yx = -0.226635376243314
rotMatrix4.Yy = 0.416625025064284
rotMatrix4.Yz = 0.880374917137714
rotMatrix4.Zx = 0.493346336172044
rotMatrix4.Zy = -0.730255087127531
rotMatrix4.Zz = 0.472585336537204
Dim translation4 As Point3d = New Point3d(-13.7771427645447, -5.04073423625269, -1.06794351266175)
workPart.ModelingViews.WorkView.SetRotationTranslationScale(rotMatrix4, translation4, 0.236784597850366)


End Sub
End Module

 
When I open the assembly, all the constraints have green checkmarks. Yet when I try to redefine any constraint, an alert pops up that warns "The constraint contains invalid geometry references..."; it will not allow me to deselect existing geometry or select new (different) geometry. It does, however, report that 2 references are selected.

I deleted the parallel constraint and the constraints associated to the blade immediately became invalid. After I reapplied the parallel constraint, it behaved normally yet appeared the same as the 'invalid' constraint.

If these constraints were applied by your journal code, I would add some debugging output to make sure the values are valid and what you expect as the code executes.

ps I notice that you are on an early release of NX6, I would encourage you to update to the latest MR for that release (I do not think this will solve your problem, but will eliminate some possibilities). I'm on NX 7.5 and I still see problems, but not exactly the same ones you do.

 
Thanks Cowski for the effort from your end.
i tried doing an update and an interpart update yesterday in my code and things are a bit better.
I will also try running my code in NX7.5 to see if the problem persists.
I will also try to add some debugging output to my code so that i can resolve the issues at my end.
I feel the solver in one of the constraints is causing the issue as it is not able to solve properly.

It is indeed going to be a long research as i donot find people putting much stuff in the open internet on automating assembly constraints.
 
After doing some research over this weekend, i was able to pinpoint the problem area. It is the way by which we are retrieving the faetures/faces to be constrained.

PFB my exaplanation:

The correct way of identifying the features to be constrained is using the Journal Identifier.

For a feature we should append "PROTO#" to the value retrieved by the feature.JournalIdentifier

For a face inside a body we should append "PROTO#.Features|Body.JournalIdentifier|Face.journalIdentifier"

and then find the object using component.FindObject(journal identifier value as string)

By doing this one will be able to create the assembly constraints perfectly :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor