Example #1
0
        public void ApplyDis(Part Assem, ComponentPositioner componentPositioner1, ConstData constinfo)
        {
            Constraint          constraint1 = componentPositioner1.CreateConstraint(true);         ///1
            ComponentConstraint compoConst  = (NXOpen.Positioning.ComponentConstraint)constraint1; ///2

            compoConst.ConstraintType = NXOpen.Positioning.Constraint.Type.Distance;               //3

            Component[]           comp     = new Component[2];
            Edge[]                edg      = new Edge[2];
            Face[]                faces    = new Face[2];
            ConstraintReference[] constref = new ConstraintReference[2];

            comp[0] = (NXOpen.Assemblies.Component)Assem.ComponentAssembly.RootComponent.FindObject(constinfo.ConstRef1[0]);
            comp[1] = (NXOpen.Assemblies.Component)Assem.ComponentAssembly.RootComponent.FindObject(constinfo.ConstRef2[0]);

            faces[0] = (NXOpen.Face)comp[0].FindObject(constinfo.ConstRef1[1]);
            faces[1] = (NXOpen.Face)comp[1].FindObject(constinfo.ConstRef2[1]);

            for (int i = 0; i < 2; i++)
            {
                constref[i] = compoConst.CreateConstraintReference(comp[i], faces[i], false, false, false);
            }

            compoConst.SetExpression("");
        }
Example #2
0
        public void loadAssemblyInfo(NXOpen.Part assemblyFilePart)
        {
            //Get root component which is top level assembly component
            rootComponent = assemblyFilePart.ComponentAssembly.RootComponent;

            //Get components and constraints in (root)assembly
            nxComponents = rootComponent.GetChildren();

            //Get constraint information
            nxPositioner  = assemblyFilePart.ComponentAssembly.Positioner;
            nxConstraints = nxPositioner.Constraints.ToArray();

            numComponents = nxComponents.Length;

            tcad_PartList         = new TransCAD.Part[numComponents];
            componentPositions    = new Point3d[numComponents];
            componentOrientations = new Matrix3x3[numComponents];
            nxCompNames           = new string[numComponents];

            for (int i = 0; i < numComponents; i++)
            {
                NXOpen.Assemblies.Component nxComponent = nxComponents[i];

                //save name of NX Part into string array
                nxCompNames[i] = nxComponent.DisplayName;

                //Get position and orientation of each part and save them into componentPositions, componentOrientations
                Point3d   componentPosition;
                Matrix3x3 componentOrientation;
                nxComponent.GetPosition(out componentPosition, out componentOrientation);
                componentPositions[i]    = componentPosition;
                componentOrientations[i] = componentOrientation;

                //Get file directory of each part in assembly
                NXOpen.Part nxPart = null;
                nxPart = (Part)nxComponent.Prototype;
                string xmlPartDir = Path.ChangeExtension(nxPart.FullPath, ".xml"); //chaneg extension from .prt to .xml

                //Load transCAD xml part file corresponding to each NX part using file name, the transCAD xml should be in same folder with same name except extension(.prt -> .xml)
                TransCAD.Part tcad_Part = tComp.CreatePart(nxComponent.DisplayName);
                tcad_Part = tComp.ImportPartFromXML(xmlPartDir, tcad_Part);
                //save transCAD part data into array "tcad_PartList"
                tcad_PartList[i] = tcad_Part;

                //Place each part in TransCAD
                double extraDisplacement = i * 100; //To see whether constraints are applied or not, intentionally, place part with extra displacement which is different from original NX part
                tComp.SetPartPlacement(tcad_Part, componentPosition.X, componentPosition.Y + extraDisplacement, componentPosition.Z - extraDisplacement,
                                       componentOrientation.Zx, componentOrientation.Zy, componentOrientation.Zz,
                                       componentOrientation.Xx, componentOrientation.Xy, componentOrientation.Xz);
                tComp.AddPart(tcad_Part);
            }
            tAssem.AddComponent(tComp);
            tAssemDoc.Update();
        }
    //------------------------------------------------------------------------------
    //  NX Startup
    //      This entry point activates the application at NX startup

    //Will work when complete path of the dll is provided to Environment Variable
    //USER_STARTUP or USER_DEFAULT
    //------------------------------------------------------------------------------
    public static int Startup()
    {
        int retValue = 0;

        try
        {
            theProgram = new Program();

            //string partPath = @"F:\TEST.prt";
            //string partPath = @"F:\CASE\ASM.prt";
            string         partPath = @"F:\CASE1\Case_1-1_stp.prt";
            PartLoadStatus PLD;
            Part           workPart = theSession.Parts.OpenBaseDisplay(partPath, out PLD) as Part;

            if (workPart != null)
            {
                ComponentAssembly assembly      = workPart.ComponentAssembly;
                Component         rootComponent = assembly.RootComponent;
                //try {object flange1 = assembly.FindObject("FLANGE(10K_150A_G)_K3029930#1");}
                //catch(Exception ex) { theUI.NXMessageBox.Show("Null", NXMessageBox.DialogType.Error, ex.Message); }

                theProgram.Traverse(rootComponent);

                Component comp1_1 = rootComponent.FindObject("COMPONENT OIL_PIPE_TYPE1_ASSY-01 2") as Component;
                Component comp1   = comp1_1.FindObject("COMPONENT FLANGE(10K_150A_G)_K3029930#1 1") as Component;
                Component comp2_1 = rootComponent.FindObject("COMPONENT OIL_PIPE_TYPE1_ASSY-01 3") as Component;
                Component comp2   = comp2_1.FindObject("COMPONENT FLANGE(10K_150A_N)_K3034074#1 1") as Component;

                Part one = comp1.Prototype as Part;
                Part two = comp2.Prototype as Part;

                DatumPlane oneXY = null;
                DatumPlane twoXY = null;

                //foreach(Component leaf in theProgram.leafList)
                //{
                //    if(leaf.DisplayName == "FLANGE(10K_150A_G)_K3029930#1")
                //    {
                //        //comp1 = leaf;
                //        one = leaf.Prototype as Part;
                //    }

                //    if (leaf.DisplayName == "FLANGE(10K_150A_N)_K3034074#1")
                //    {
                //        //comp2 = leaf;
                //        two = leaf.Prototype as Part;
                //    }
                //}

                if (one != null && two != null)
                {
                    theProgram.GetXYPlane(one, out oneXY);
                    theProgram.GetXYPlane(two, out twoXY);

                    oneXY = comp1.FindOccurrence(oneXY) as DatumPlane;
                    twoXY = comp2.FindOccurrence(twoXY) as DatumPlane;
                }

                //Tag tag1 = theUfSession.Assem.AskPartOccurrence(oneXY.Tag);

                try
                {
                    ComponentPositioner positioner1 = assembly.Positioner;
                    positioner1.BeginAssemblyConstraints();

                    Constraint          constraint1          = positioner1.CreateConstraint(true);
                    ComponentConstraint componentConstraint1 = constraint1 as ComponentConstraint;

                    componentConstraint1.ConstraintAlignment = Constraint.Alignment.ContraAlign;
                    componentConstraint1.ConstraintType      = Constraint.Type.Touch;
                    ConstraintReference ref1 = componentConstraint1.CreateConstraintReference(comp1, oneXY, false, false, false);
                    ConstraintReference ref2 = componentConstraint1.CreateConstraintReference(comp2, twoXY, false, false, false);
                    //componentConstraint1.FlipAlignment();
                    //componentConstraint1.SetAlignmentHint(NXOpen.Positioning.Constraint.Alignment.ContraAlign);
                }
                catch (Exception E)
                {
                    theUI.NXMessageBox.Show("Error", NXMessageBox.DialogType.Error, E.Message);
                }
            }


            PartSaveStatus ps;
            bool           partSaved;
            theSession.Parts.SaveAll(out partSaved, out ps);

            theUfSession.Part.CloseAll();
        }
        catch (NXOpen.NXException ex)
        {
            // ---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("UI Styler", NXMessageBox.DialogType.Error, ex.Message);
        }
        return(retValue);
    }
Example #4
0
        public void ToNXFile(Session nxSession)
        {
            NXOpen.Part nxSubAssem;
            var         newFile = new NewNXfile();
            var         fileDir = new NewNXfile();

            for (int i = 0; i < compCount; i++)
            {
                nxRootAssem = nxSession.Parts.Work;
                if (compName[i] == "Default SubAssembly")
                {
                    continue;
                }
                NXOpen.FileNew compFile = nxSession.Parts.FileNew();
                newFile.NXfile(compFile, fileDir.subassemdir(compName[i]));
                compFile.MakeDisplayedPart = false;
                NXOpen.Assemblies.CreateNewComponentBuilder createNewComponentBuilder1;
                createNewComponentBuilder1 = nxRootAssem.AssemblyManager.CreateNewComponentBuilder();
                createNewComponentBuilder1.NewComponentName = compName[i];
                createNewComponentBuilder1.ReferenceSet     = NXOpen.Assemblies.CreateNewComponentBuilder.ComponentReferenceSetType.EntirePartOnly;
                createNewComponentBuilder1.ReferenceSetName = "Entire Part";
                createNewComponentBuilder1.NewFile          = compFile;
                NXOpen.NXObject nXObject_SubAssem = createNewComponentBuilder1.Commit();
                createNewComponentBuilder1.Destroy();
                nxSubAssem = (NXOpen.Part)nxSession.Parts.FindObject(compName[i]);
                NXObject[] nxGeom = new NXObject[partCount];
                for (int j = 0; j < partCount; j++)
                {
                    NXOpen.BasePart       basePart1;
                    NXOpen.PartLoadStatus partLoadStatus, partLoadStatus2;
                    basePart1 = nxSession.Parts.OpenBase(fileDir.partdir(partFileName[j]), out partLoadStatus);
                    partLoadStatus.Dispose();
                    basepoint1      = new NXOpen.Point3d(0.0, 0.0, 0.0);
                    orientation1    = new NXOpen.Matrix3x3();
                    orientation1.Xx = 1.0;
                    orientation1.Xy = 0.0;
                    orientation1.Xz = 0.0;
                    orientation1.Yx = 0.0;
                    orientation1.Yy = 1.0;
                    orientation1.Yz = 0.0;
                    orientation1.Zx = 0.0;
                    orientation1.Zy = 0.0;
                    orientation1.Zz = 1.0;
                    NXOpen.Assemblies.Component component1;
                    component1 = nxRootAssem.ComponentAssembly.AddComponent(fileDir.partdir(partFileName[j]), "MODEL", "A1-1", basepoint1, orientation1, -1, out partLoadStatus2, true);
                    partLoadStatus2.Dispose();
                    NXOpen.Assemblies.Component[] origComponents1 = new NXOpen.Assemblies.Component[1];
                    origComponents1[0] = component1;
                    NXOpen.Assemblies.Component   component2 = (NXOpen.Assemblies.Component)nXObject_SubAssem;
                    NXOpen.Assemblies.Component[] newComponents1;
                    NXOpen.ErrorList errorList1;
                    nxSubAssem.ComponentAssembly.RestructureComponents(origComponents1, component2, true, out newComponents1, out errorList1);
                    errorList1.Dispose();
                    nxGeom[i] = origComponents1[0];
                    string reef = nxGeom[i].OwningComponent.DisplayName;
                    Console.WriteLine("*************************" + reef);
                }
                /*Working Information with Hardcoded information;*/
                ComponentPositioner[]         tempcomponentPositioner1 = new ComponentPositioner[2];
                ComponentConstraint[]         tempcomponentConstraint1 = new ComponentConstraint[2];
                ComponentPositioner[]         componentPositioner1     = new ComponentPositioner[2];
                ComponentPositioner[]         componentPositioner2     = new ComponentPositioner[2];
                ComponentConstraint[]         componentConstraint1     = new ComponentConstraint[2];
                ComponentConstraint[]         componentConstraint2     = new ComponentConstraint[2];
                NXOpen.Assemblies.Component[] component01 = new Component[2];
                NXOpen.Assemblies.Component[] component02 = new Component[2];
                for (int m = 0; m < 2; m++)
                {
                    component01[m] = (NXOpen.Assemblies.Component)nxSubAssem.ComponentAssembly.RootComponent.FindObject("COMPONENT " + partname[m] + " 1");
                    //NXOpen.Assemblies.Component component02 = (NXOpen.Assemblies.Component)nxSubAssem.ComponentAssembly.RootComponent.FindObject("COMPONENT " + partname[0] + " 1");
                }

                /*
                 * tempcomponentPositioner1[0] = nxSubAssem.ComponentAssembly.Positioner;
                 * tempcomponentPositioner1[0].BeginAssemblyConstraints();
                 * Constraint tempconstraint1;
                 * tempconstraint1 = tempcomponentPositioner1[0].CreateConstraint(true);
                 * tempcomponentConstraint1[0] = (NXOpen.Positioning.ComponentConstraint)tempconstraint1;
                 * tempcomponentConstraint1[0].ConstraintType = NXOpen.Positioning.Constraint.Type.Concentric;
                 * Edge edge1 = (NXOpen.Edge)component01[0].FindObject(EdgeNames[0]);
                 * ConstraintReference ConsRef1 = tempcomponentConstraint1[0].CreateConstraintReference(component01[0],edge1,false,false,false);
                 * Edge edge2 = (NXOpen.Edge)component01[1].FindObject(EdgeNames[1]);
                 * ConstraintReference ConsRef2 = tempcomponentConstraint1[0].CreateConstraintReference(component01[1], edge2, false, false, false);
                 */


                for (int k = 0; k < 2; k++)
                {
                    if (constNamesTemp[k].Contains("Coaxial"))
                    {
                        componentPositioner1[k] = nxSubAssem.ComponentAssembly.Positioner;
                        componentPositioner1[k].BeginAssemblyConstraints();
                        Constraint constraint1;
                        constraint1             = componentPositioner1[k].CreateConstraint(true);
                        componentConstraint1[k] = (NXOpen.Positioning.ComponentConstraint)constraint1;
                        //componentConstraint1[k].ConstraintAlignment = NXOpen.Positioning.Constraint.Alignment.ContraAlign;
                        componentConstraint1[k].ConstraintType = NXOpen.Positioning.Constraint.Type.Fit;
                        NXOpen.Face face1 = (NXOpen.Face)component01[0].FindObject(ConstNames1[k]);
                        NXOpen.Positioning.ConstraintReference constraintReference1;
                        constraintReference1 = componentConstraint1[k].CreateConstraintReference(component01[0], face1, false, false, false);
                        component01[1]       = (NXOpen.Assemblies.Component)nxSubAssem.ComponentAssembly.RootComponent.FindObject("COMPONENT " + partname[1] + " 1");
                        NXOpen.Face face2 = (NXOpen.Face)component01[1].FindObject(ConstNames2[k]);
                        //Face face3 = (NXOpen.Face)component01[1].FindObject(ConstNames2[1]);
                        NXOpen.Positioning.ConstraintReference constraintReference2;
                        constraintReference2 = componentConstraint1[k].CreateConstraintReference(component01[1], face2, false, false, false);
                    }
                    else
                    {
                        componentPositioner2[k] = nxSubAssem.ComponentAssembly.Positioner;
                        componentPositioner2[k].BeginAssemblyConstraints();
                        Constraint constraint1;
                        constraint1             = componentPositioner2[k].CreateConstraint(true);
                        componentConstraint2[k] = (NXOpen.Positioning.ComponentConstraint)constraint1;
                        //componentConstraint2[k].ConstraintAlignment = NXOpen.Positioning.Constraint.Alignment.InferAlign;
                        componentConstraint2[k].ConstraintType = NXOpen.Positioning.Constraint.Type.Touch;
                        NXOpen.Face face1 = (NXOpen.Face)component01[0].FindObject(ConstNames1[k]);
                        NXOpen.Positioning.ConstraintReference constraintReference3;
                        constraintReference3 = componentConstraint2[k].CreateConstraintReference(component01[0], face1, false, false, false);
                        component01[1]       = (NXOpen.Assemblies.Component)nxSubAssem.ComponentAssembly.RootComponent.FindObject("COMPONENT " + partname[1] + " 1");
                        NXOpen.Face face2 = (NXOpen.Face)component01[1].FindObject(ConstNames2[k]);
                        NXOpen.Positioning.ConstraintReference constraintReference4;
                        constraintReference4 = componentConstraint2[k].CreateConstraintReference(component01[1], face2, false, false, false);
                        constraintReference4.SetFixHint(true);
                    }
                }
                break;
            }
            PartSaveStatus fileSave;

            fileSave = nxRootAssem.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False);
        }