//C#
//Bruce Harrison
//12-21-2011
using NXOpen.Assemblies;
using NXOpen.Positioning;
using NXOpenUI;
using NXOpen.UF;
/// <summary>
/// Overly complex method for constraining objects!
/// </summary>
/// <param name="Component1">Newest part to be added to assembly</param>
/// <param name="basePart1">Assembly that will be added to</param>
public void CreateConstraints(Component Component1, BasePart basePart1)
{
Session MainSession = Session.GetSession();
UI UISession = UI.GetUI();
Part WorkPart = null;
//Object array that will save the selected objects
NXObject[] NO = null;
WorkPart = MainSession.Parts.Work;
WorkPart.Views.WorkView.RenderingStyle = NXOpen.View.RenderingStyleType.ShadedWithEdges;
//Masks that define what is selectable
var MaskCollection = CreateMask();
ComponentPositioner CP1 = WorkPart.ComponentAssembly.Positioner;
CP1.ClearNetwork();
Arrangement Arrangement1 = (Arrangement)WorkPart.ComponentAssembly.Arrangements.FindObject("Arrangement 1");
CP1.PrimaryArrangement = Arrangement1;
CP1.BeginAssemblyConstraints();
MainSession.Preferences.Assemblies.InterpartPositioning = true;
Network Network1 = CP1.EstablishNetwork();
ComponentNetwork CN1 = (ComponentNetwork)Network1;
CN1.DisplayComponent = null;
CN1.NetworkArrangementsMode = ComponentNetwork.ArrangementsMode.Existing;
CN1.MoveObjectsState = true;
//Startup the object selector dialog. need to find a way to limit selection to 2 edges.
UISession.SelectionManager.SelectObjects("Select 2 Circular Edges To Join", "Select 2 Edges", Selection.SelectionScope.AnyInAssembly, Selection.SelectionAction.ClearAndEnableSpecific, true,false, MaskCollection, out NO);
Constraint Constraint1 = CP1.CreateConstraint();
ComponentConstraint CConstraint1 = (ComponentConstraint)Constraint1;
Constraint1.ConstraintType = Constraint.Type.Concentric;
//Begin constraint on first edge
Edge Face1 = (Edge)NO[0];
var Constraint1Ref = CConstraint1.CreateConstraintReference(Component1, Face1, false, false, false);
//Make this edge stationary, the second will move to meet this one
Constraint1Ref.SetFixHint(true);
//Define constraint edge on second surface
Edge Face2 = (Edge)NO[1];
var Constraint2Ref = CConstraint1.CreateConstraintReference(basePart1, Face2, false, false, false);
//flip constraint 180
CConstraint1.FlipAlignment();
//compute constraints
CN1.Solve();
//If part is 180 off, this message box give you the option of flipping it. Need to find a way to see if one part is
//colliding with another to automate this.
var Response = MessageBox.Show("Reverse direction of constraint?", "Reverse Constraint?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (Response == DialogResult.Yes)
{
CConstraint1.FlipAlignment();
CN1.Solve();
}
//throw away all the extra junk
CP1.ClearNetwork();
CP1.DeleteNonPersistentConstraints();
CP1.EndAssemblyConstraints();
}
/// <summary>
/// Setup the masks required for manual selection
/// </summary>
/// <returns>Collection of MaskTriple(s)</returns>
private Selection.MaskTriple[] CreateMask()
{
var Mask1 = new Selection.MaskTriple();
Mask1.Type = UFConstants.UF_solid_type;
Mask1.Subtype = UFConstants.UF_solid_body_subtype;
Mask1.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_ANY_EDGE;
//Add masks to the masktriple collection of masks
Selection.MaskTriple[] MaskCollection = { Mask1 };
return MaskCollection;
}