public static void addPartToAssembly(string fileNameToLoad)
        {
            IADAssemblySession objAssmSession;     //Holds Alibre Assembly Session Object
            IADOccurrence objADRootOccurrence;     //Holds Root Occurrence of the Assembly
            IADOccurrences objADOccurrences;       //Holds all Occurrences of the Assembly
            string destinationString;              //Holds the location where the File gets Saved
            bool flag = true;


            if (rootObj == null || rootObj.Sessions == null)  //Exit if for some reason an instance of Alibre Design could not be found
                return;

            if (rootObj.Sessions.Count > 0)         // If there is atleast one workspace open
            {
                foreach (IADSession objSession in rootObj.Sessions)
                {
                    if ((objSession.SessionType == ADObjectSubType.AD_ASSEMBLY) && flag)     //If there is atleast one Assembly open
                    {
                        objAssmSession = (IADAssemblySession)objSession;                                 //part is inserted into that assembly
                        flag = false;

                        Console.WriteLine("Inserting Part into the Assembly...");

                        //Get Root Occurrence from Assembly Session
                        objADRootOccurrence = objAssmSession.RootOccurrence;

                        //Get Occurrences collection from Root Occurance
                        objADOccurrences = objADRootOccurrence.Occurrences;

                        //Holds Geometry Factory and et Geometry Factory from Session object
                        IADGeometryFactory objADGeometryFactory = objSession.GeometryFactory;

                        //Populate the Transformation Array with the following Data for Back View
                        double[] adblTransformationArrayData = new double[16] {1,0,0,0,
                                                                               0,1,0,0,
                                                                               0,0,1,0,
                                                                               0,0,0,1};

                        //Create Transformation
                        Array sysArray = adblTransformationArrayData;
                        IADTransformation objADTransformation = objADGeometryFactory.CreateTransform(ref sysArray);

                        //Add an Empty Part as Occurrence
                        //objADOccurrence = objADOccurrences.AddEmptyPart("BlockWithHole", false, objADTransformation);
                        object ParttoAddToAssemblyFullPath = fileNameToLoad;
                        objADOccurrence = objADOccurrences.Add(ref ParttoAddToAssemblyFullPath, objADTransformation);
                        //Set Design Session to be the empty Part's Design Session that was just added to the assembly
                        DesignSession = objADOccurrence.DesignSession;

                        //Set Part Session to be the empty Part's Design Session that was just added to the assembly
                        objPartSession = (IADPartSession)DesignSession;

                        //Call to CreateFeatures method to add features to the empty part inserted into the assembly
                        //CreateFeatures();

                        //lblStatus.Text = "Part inserted successfully into " + objSession.Name;

                        ////Saves the Assembly with the Part to the location specified
                        //lblStatus.Text = "Saving assembly on C:\\ Drive...";
                        //destinationString = "C://";
                        //object saveLocation = (object)destinationString;
                        //try
                        //{
                        //    objSession.SaveAs(ref saveLocation, objSession.Name);
                        //    lblStatus.Text = "Assembly is saved successfully on C:\\";
                        //    btnInsert.Enabled = false;
                        //}
                        //catch
                        //{
                        //    lblStatus.Text = "Assembly in use, unable to save.";
                        //}

                        break;
                    }
                    else
                        Console.WriteLine("Please open any Assembly");

                }
            }
            else        //If there is no assembly open
                Console.WriteLine("Please open any Assembly");
        }
        private void CreateCAM(IADAssemblySession assembly, string name, bool secondCam)
        {
            IADTransformation trans      = assembly.GeometryFactory.CreateIdentityTransform();
            IADOccurrence     occurrence = assembly.RootOccurrence.Occurrences.AddEmptyPart(name, isSheetMetal: false, pTransform: trans);
            IADPartSession    part       = (IADPartSession)occurrence.DesignSession;


            IADDesignPlane xyPlane = part.DesignPlanes.Item("XY-Plane");
            IADSketch      sketch  = part.Sketches.AddSketch(null, part.planeXY(), "CAM Profile");

            // Create the spline
            int   order        = 3;
            int   splinePoints = cam.camPoints.Count + 1;
            Array points       = new double [splinePoints * 2];
            Array knots        = new double[splinePoints + order];
            Array weights      = new double[splinePoints];

            int idx = 0;

            for (int i = 0; i < cam.camPoints.Count; i++)
            {
                points.SetValue(MMToCM(cam.camPoints[i].x), idx * 2);
                points.SetValue(MMToCM(cam.camPoints[i].y), idx * 2 + 1);
                weights.SetValue(0, idx);
                idx++;
                //if (i == 0 )
                //{
                //    points.SetValue(cam.camPoints[i].x, idx * 2);
                //    points.SetValue(cam.camPoints[i].y, idx * 2 + 1);
                //    weights.SetValue(0, idx);
                //    idx++;
                //}
            }
            // Add the first point again to close the spline
            points.SetValue(points.GetValue(0), idx * 2);
            points.SetValue(points.GetValue(1), idx * 2 + 1);
            weights.SetValue(0, idx);
            idx++;
            //points.SetValue(cam.camPoints[0].x, idx * 2);
            //points.SetValue(cam.camPoints[0].y, idx * 2 + 1);
            //weights.SetValue(0, idx);
            //idx++;

            idx = 0;
            for (int i = 0; i < splinePoints - order + 1; i++)
            {
                knots.SetValue((float)i / (float)(splinePoints - order + 1), i + order - 1);
            }
            for (int i = 0; i < order; i++)
            {
                knots.SetValue(0, i);
                knots.SetValue(1, splinePoints + i);
            }
            sketch.BeginChange();
            sketch.Figures.AddBspline(order, splinePoints, ref points, ref knots, ref weights);
            sketch.EndChange();

            // Extrude the cam
            double draftAngle = 0;

            part.Features.AddExtrudedBoss(pSketch: sketch,
                                          depth: MMToCM(cam.CamThickness),
                                          endCondition: ADPartFeatureEndCondition.AD_TO_DEPTH,
                                          toGeometryOcc: null,
                                          toGeometryObject: null,
                                          toGeometryOffset: 0,
                                          direction: ADDirectionType.AD_ALONG_NORMAL,
                                          pDirectionOcc: null,
                                          pDirectionObject: null,
                                          isDirectionReversed: false,
                                          draftAngle: draftAngle,
                                          IsOutwardDraft: false,
                                          name: "CAM Boss");

            // Create the bore
            sketch = part.Sketches.AddSketch(null, part.planeXY(), "Bore ");
            sketch.BeginChange();
            sketch.Figures.AddCircle(0, 0, MMToCM(cam.EccentricBearingOuterDia / 2.0));
            sketch.EndChange();
            part.Features.AddExtrudedCutout(pSketch: sketch,
                                            depth: 0,
                                            endCondition: ADPartFeatureEndCondition.AD_THROUGH_ALL,
                                            toGeometryOcc: null,
                                            toGeometryObject: null,
                                            toGeometryOffset: 0,
                                            direction: ADDirectionType.AD_ALONG_NORMAL,
                                            pDirectionOcc: null,
                                            pDirectionObject: null,
                                            isDirectionReversed: false,
                                            draftAngle: draftAngle,
                                            IsOutwardDraft: false,
                                            name: "Bore cutout");

            double offsetAngle = 0;

            if (secondCam)
            {
                offsetAngle = (Math.PI * 2.0) / (cam.TeethInCAM + 1) / 2;
            }

            // Create the output holes
            sketch = part.Sketches.AddSketch(null, part.planeXY(), "Output Holes");
            sketch.BeginChange();
            if (cam.OutputBearings > 0)
            {
                double holeDia  = MMToCM(cam.OutputBearingsDia + cam.Eccentricity * 2);
                double pitchDia = MMToCM(cam.OutputPitchCircleDia);
                for (int i = 0; i < cam.OutputBearings; i++)
                {
                    double angle = Math.PI * 2.0 * (double)i / (double)cam.OutputBearings - offsetAngle;
                    double x     = (pitchDia / 2) * Math.Cos(angle);
                    double y     = (pitchDia / 2) * Math.Sin(angle);


                    sketch.Figures.AddCircle(x, y, holeDia / 2.0);
                }
            }
            sketch.EndChange();
            part.Features.AddExtrudedCutout(pSketch: sketch,
                                            depth: 0,
                                            endCondition: ADPartFeatureEndCondition.AD_THROUGH_ALL,
                                            toGeometryOcc: null,
                                            toGeometryObject: null,
                                            toGeometryOffset: 0,
                                            direction: ADDirectionType.AD_ALONG_NORMAL,
                                            pDirectionOcc: null,
                                            pDirectionObject: null,
                                            isDirectionReversed: false,
                                            draftAngle: draftAngle,
                                            IsOutwardDraft: false,
                                            name: "Output cutout");


            //object filename = @"c:\temp\ad";
            //part.SaveAs(ref filename,"name");
        }
 public static IADDesignPlane planeXY(this IADPartSession part)
 {
     return(part.DesignPlanes.Item("XY-Plane"));
 }
        private void CreateBase(IADAssemblySession assembly, string name)
        {
            IADTransformation trans      = assembly.GeometryFactory.CreateIdentityTransform();
            IADOccurrence     occurrence = assembly.RootOccurrence.Occurrences.AddEmptyPart(name, isSheetMetal: false, pTransform: trans);
            IADPartSession    part       = (IADPartSession)occurrence.DesignSession;

            IADDesignPlane xyPlane = part.DesignPlanes.Item("XY-Plane");
            IADSketch      sketch  = part.Sketches.AddSketch(null, part.planeXY(), "Pin");

            // Create circle for the pin

            sketch.BeginChange();
            foreach (Pt p in cam.rollerPoints)
            {
                sketch.Figures.AddCircle(MMToCM(p.x), MMToCM(p.y), MMToCM(cam.RollerDiameter / 2.0));
            }
            sketch.EndChange();
            part.Features.AddExtrudedBoss(pSketch: sketch,
                                          depth: MMToCM(PinHeight),
                                          endCondition: ADPartFeatureEndCondition.AD_TO_DEPTH,
                                          toGeometryOcc: null,
                                          toGeometryObject: null,
                                          toGeometryOffset: 0,
                                          direction: ADDirectionType.AD_ALONG_NORMAL,
                                          pDirectionOcc: null,
                                          pDirectionObject: null,
                                          isDirectionReversed: false,
                                          draftAngle: null,
                                          IsOutwardDraft: false,
                                          name: "Pin");

            // Base body
            sketch = part.Sketches.AddSketch(null, part.planeXY(), "Base");

            sketch.BeginChange();
            sketch.Figures.AddCircle(0, 0, MMToCM(cam.ComputedPinBoltCircleDiameter / 2 + cam.RollerDiameter));
            sketch.EndChange();

            part.Features.AddExtrudedBoss(pSketch: sketch,
                                          depth: MMToCM(BaseThickness),
                                          endCondition: ADPartFeatureEndCondition.AD_TO_DEPTH,
                                          toGeometryOcc: null,
                                          toGeometryObject: null,
                                          toGeometryOffset: 0,
                                          direction: ADDirectionType.AD_ALONG_NORMAL,
                                          pDirectionOcc: null,
                                          pDirectionObject: null,
                                          isDirectionReversed: true,
                                          draftAngle: null,
                                          IsOutwardDraft: false,
                                          name: "Base");

            // Create the bore for the motor
            sketch = part.Sketches.AddSketch(null, part.planeXY(), "Motor Mount Bore ");
            sketch.BeginChange();
            sketch.Figures.AddCircle(0, 0, MMToCM(motor.motorMountBore / 2.0));
            sketch.EndChange();
            part.Features.AddExtrudedCutout(pSketch: sketch,
                                            depth: 0,
                                            endCondition: ADPartFeatureEndCondition.AD_THROUGH_ALL,
                                            toGeometryOcc: null,
                                            toGeometryObject: null,
                                            toGeometryOffset: 0,
                                            direction: ADDirectionType.AD_ALONG_NORMAL,
                                            pDirectionOcc: null,
                                            pDirectionObject: null,
                                            isDirectionReversed: false,
                                            draftAngle: null,
                                            IsOutwardDraft: false,
                                            name: "Motor Mount Bore");

            // Create the CS mounting holes for the motor
            sketch = part.Sketches.AddSketch(null, part.planeXY(), "Motor Mounting Holes");
            sketch.BeginChange();
            for (int i = 0; i < motor.mountingHoles; i++)
            {
                sketch.Figures.AddSketchPoint(MMToCM(Math.Cos(2 * Math.PI * i / motor.mountingHoles) * motor.mountingHolePitchCircle / 2),
                                              MMToCM(Math.Sin(2 * Math.PI * i / motor.mountingHoles) * motor.mountingHolePitchCircle / 2));
            }
            sketch.EndChange();
            part.Features.AddCounterSunkHole(pSketch: sketch,
                                             depth: 0,
                                             diameter: MMToCM(motor.mountingHoleDia),
                                             counterSinkDiameter: MMToCM(motor.mountingHoleCSDia),
                                             counterSinkAngle: Math.PI / 2,
                                             isReversed: false,
                                             tappedThread: null,
                                             depthCondition: ADHoleDepthCondition.AD_HOLE_THROUGH_ALL,
                                             toGeometryOcc: null,
                                             toGeometryObject: null,
                                             toGeometryOffset: 0,
                                             name: "Motor mounting holes");

            //object filename = @"c:\temp\ad";
            //part.SaveAs(ref filename, "name");
        }
        private void CreateEccentric(IADAssemblySession assembly, string name)
        {
            IADTransformation trans      = assembly.GeometryFactory.CreateIdentityTransform();
            IADOccurrence     occurrence = assembly.RootOccurrence.Occurrences.AddEmptyPart(name, isSheetMetal: false, pTransform: trans);
            IADPartSession    part       = (IADPartSession)occurrence.DesignSession;
            IADDesignPlane    xyPlane    = part.DesignPlanes.Item("XY-Plane");

            //IADParameter eccentricParam = part.Parameters.NewParameter("Eccentric", ADParameterType.AD_DISTANCE);
            //IADParameter bearingInnerDiaParam = part.Parameters.NewParameter("BearingInnerDia", ADParameterType.AD_DISTANCE);
            //part.Parameters.OpenParameterTransaction();
            //eccentricParam.Value = MMToCM(cam.Eccentricity);
            //bearingInnerDiaParam.Value = MMToCM(BearingID);
            //part.Parameters.CloseParameterTransaction();

            //IADParameter eccentricyParam = part.Parameters.NewParameter("Eccentricy", ADParameterType.AD_DISTANCE);
            //part.Parameters.OpenParameterTransaction();
            //eccentricyParam.ExternallyDriven = true;
            //eccentricyParam.SourceDocumentID = "GlobalParameters";
            //eccentricyParam.SourceItemID = "Eccentricy";
            //part.Parameters.CloseParameterTransaction();

            // Create circle for the pin
            IADSketch sketch = part.Sketches.AddSketch(null, part.planeXY(), "Eccentric");

            sketch.BeginChange();
            IADSketchCircle circle = sketch.Figures.AddCircle(MMToCM(cam.Eccentricity), 0, MMToCM(cam.EccentricBearingInnerDia / 2.0));

            //IADDimension dim = sketch.Dimensions.PlaceDiametricDimension(circle, "BearingInnerDia");
            sketch.EndChange();
            part.Features.AddExtrudedBoss(pSketch: sketch,
                                          depth: MMToCM(PinHeight),
                                          endCondition: ADPartFeatureEndCondition.AD_TO_DEPTH,
                                          toGeometryOcc: null,
                                          toGeometryObject: null,
                                          toGeometryOffset: 0,
                                          direction: ADDirectionType.AD_ALONG_NORMAL,
                                          pDirectionOcc: null,
                                          pDirectionObject: null,
                                          isDirectionReversed: true,
                                          draftAngle: null,
                                          IsOutwardDraft: false,
                                          name: "Eccentric");

            sketch = part.Sketches.AddSketch(null, part.planeXY(), "Centered");
            sketch.BeginChange();
            circle = sketch.Figures.AddCircle(0, 0, MMToCM(cam.EccentricBearingInnerDia / 2.0));
            //dim = sketch.Dimensions.PlaceDiametricDimension(circle, "BearingInnerDia");
            sketch.EndChange();
            part.Features.AddExtrudedBoss(pSketch: sketch,
                                          depth: MMToCM(PinHeight),
                                          endCondition: ADPartFeatureEndCondition.AD_TO_DEPTH,
                                          toGeometryOcc: null,
                                          toGeometryObject: null,
                                          toGeometryOffset: 0,
                                          direction: ADDirectionType.AD_ALONG_NORMAL,
                                          pDirectionOcc: null,
                                          pDirectionObject: null,
                                          isDirectionReversed: false,
                                          draftAngle: null,
                                          IsOutwardDraft: false,
                                          name: "Pin");

            // Create the bore for the motor shaft
            sketch = part.Sketches.AddSketch(null, part.planeXY(), "Motor shaft Bore ");
            sketch.BeginChange();
            circle = sketch.Figures.AddCircle(0, 0, MMToCM(motor.shaftDia / 2.0));
            //dim = sketch.Dimensions.PlaceDiametricDimension(circle, "DriveShaftDia");
            sketch.EndChange();
            part.Features.AddExtrudedCutout(pSketch: sketch,
                                            depth: 0,
                                            endCondition: ADPartFeatureEndCondition.AD_THROUGH_ALL,
                                            toGeometryOcc: null,
                                            toGeometryObject: null,
                                            toGeometryOffset: 0,
                                            direction: ADDirectionType.AD_ALONG_NORMAL,
                                            pDirectionOcc: null,
                                            pDirectionObject: null,
                                            isDirectionReversed: false,
                                            draftAngle: null,
                                            IsOutwardDraft: false,
                                            name: "Motor shaft Bore");

            //object filename = @"c:\temp\ad";
            //part.SaveAs(ref filename, "name");
        }