Ejemplo n.º 1
0
        private void CreateInvLayout()
        {
            CreateLayoutPartFile();

            Inventor.AssemblyComponentDefinition componentDefinition = InventorServices.Persistence.PersistenceManager.ActiveAssemblyDoc.ComponentDefinition;
            TransformationMatrix = InventorApplication.TransientGeometry.CreateMatrix();
            ComponentOccurrence  componentOccurrence = componentDefinition.Occurrences.Add(LayoutPartPath, TransformationMatrix);
            ComponentOccurrences occurrences         = componentDefinition.Occurrences;

            //TODO This is janky.  Don't need to assume that we are starting in an empty assembly file.
            ComponentOccurrence     layoutOccurrence          = occurrences[1];
            PartComponentDefinition layoutComponentDefinition = (PartComponentDefinition)layoutOccurrence.Definition;

            for (int i = 0; i < ModulePoints.Count; i++)
            {
                WorkPoint workPoint = layoutComponentDefinition.WorkPoints.AddFixed(ModulePoints[i], false);
                workPoint.Grounded = true;
                workPoint.Visible  = false;
                //Inventor's API documentation is so bad!
                object workPointProxyObject;
                layoutOccurrence.CreateGeometryProxy(workPoint, out workPointProxyObject);
                LayoutWorkPointProxies.Add((WorkPointProxy)workPointProxyObject);
                LayoutWorkPoints.Add(workPoint);
            }

            LayoutWorkPlane          = layoutComponentDefinition.WorkPlanes.AddByThreePoints(layoutWorkPoints[0], layoutWorkPoints[1], layoutWorkPoints[2]);
            LayoutWorkPlane.Grounded = true;
            LayoutWorkPlane.Visible  = false;
            object wPlaneProxyObject;

            layoutOccurrence.CreateGeometryProxy(LayoutWorkPlane, out wPlaneProxyObject);
            ModuleWorkPlaneProxyAssembly = (WorkPlaneProxy)wPlaneProxyObject;
        }
Ejemplo n.º 2
0
        public double[] FindOrigin(ComponentOccurrence oCompOccur)
        {
            UnitsOfMeasure oUOM = _invApp.ActiveDocument.UnitsOfMeasure;
            AssemblyComponentDefinition oCompDef = (AssemblyComponentDefinition)oCompOccur.Definition;
            object oWorkPointProxy;

            double[]  c   = new double[3];
            WorkPoint oWP = oCompDef.WorkPoints[1];

            oCompOccur.CreateGeometryProxy(oWP, out oWorkPointProxy);

            c[0] = ((WorkPointProxy)oWorkPointProxy).Point.X;
            c[1] = ((WorkPointProxy)oWorkPointProxy).Point.Y;
            c[2] = ((WorkPointProxy)oWorkPointProxy).Point.Z;

            for (int k = 0; k < 3; k++)
            {
                c[k] = oUOM.ConvertUnits(c[k], "cm", "m");
            }

            string AbsolutePosition, name;

            name = FormatName(oCompOccur.Name);

            return(c);
        }
Ejemplo n.º 3
0
        private void insertPartsAndMateEdges()
        {
            // create an assembly
            AssemblyDocument            oAssDoc = (AssemblyDocument)mApp.Documents.Add(DocumentTypeEnum.kAssemblyDocumentObject);
            AssemblyComponentDefinition oAssDef = oAssDoc.ComponentDefinition;

            Matrix oM = mApp.TransientGeometry.CreateMatrix();

            //place the two parts
            ComponentOccurrence oOcc1 = oAssDef.Occurrences.Add("c:\\temp\\test1.ipt", oM);
            ComponentOccurrence oOcc2 = oAssDef.Occurrences.Add("c:\\temp\\test2.ipt", oM);

            // find the two faces to mate
            PartDocument     oDoc1          = (PartDocument)oOcc1.Definition.Document;
            ObjectCollection oObjCollection = oDoc1.AttributeManager.FindObjects("demoAttset", "demoAtt");

            Face oFace1 = null;

            if (oObjCollection[1] is Face)
            {
                oFace1 = (Face)oObjCollection[1];
            }

            PartDocument oDoc2 = (PartDocument)oOcc2.Definition.Document;

            oObjCollection = oDoc2.AttributeManager.FindObjects("demoAttset", "demoAtt");

            Face oFace2 = null;

            if (oObjCollection[1] is Face)
            {
                oFace2 = (Face)oObjCollection[1];
            }

            Object tempObj;

            //create the proxy objects for the two faces

            oOcc1.CreateGeometryProxy(oFace1, out tempObj);
            FaceProxy oAsmProxyFace1 = (FaceProxy)tempObj;

            oOcc2.CreateGeometryProxy(oFace2, out tempObj);
            FaceProxy oAsmProxyFace2 = (FaceProxy)tempObj;

            // add the mate constraint
            oAssDef.Constraints.AddMateConstraint(oAsmProxyFace1, oAsmProxyFace2, 0);
        }
Ejemplo n.º 4
0
        // This finds the entity associated with
        // an iMate of a specified name.  This
        // allows iMates to be used as a generic
        // naming mechansim.
        private object GetNamedEntity
            (ComponentOccurrence Occurrence, string Name)
        {
            // Look for the iMate that has the
            // specified name in the referenced file.
            PartComponentDefinition partDef =
                (PartComponentDefinition)Occurrence.Definition;

            object resultEntity = null;

            resultEntity = null;

            foreach (iMateDefinition iMate
                     in partDef.iMateDefinitions)
            {
                // Check to see if this iMate has
                // the correct name
                if (iMate.Name.ToUpper() ==
                    Name.ToUpper())
                {
                    // Get the geometry associated
                    // with the iMate. Using InvokeMember
                    // because the iMateDefinition is the
                    // base class and does not have an
                    // Entity property
                    object entity = null;
                    entity = iMate.GetType().InvokeMember
                                 ("Entity",
                                 BindingFlags.GetProperty,
                                 null, iMate, null);

                    // Create a proxy.
                    Occurrence.CreateGeometryProxy
                        (entity, out resultEntity);
                    break;
                }
            }

            // Return the found entity, or Nothing if a match wasn't found.
            return(resultEntity);
        }
Ejemplo n.º 5
0
        internal void PlaceWorkGeometryForContsraints(PartComponentDefinition layoutComponentDefinition, ComponentOccurrence layoutOccurrence, int moduleNumber)
        {
            PartDocument        partDoc       = (PartDocument)layoutComponentDefinition.Document;
            ReferenceKeyManager refKeyManager = partDoc.ReferenceKeyManager;

            //TODO: Property ModuleId can be factored out and _binder.ContextData can be used instead.
            ModuleId = moduleNumber;

            for (int i = 0; i < PointObjects.Count; i++)
            {
                //Each ModuleObject needs to have its ContextManager set.
                PointObjects[i].Binder.ContextManager.BindingContextManager = refKeyManager;
                WorkPoint workPoint;
                if (PointObjects[i].Binder.GetObjectFromTrace <WorkPoint>(out workPoint))
                {
                    Inventor.Point newLocation = PersistenceManager.InventorApplication.TransientGeometry.CreatePoint(InternalModulePoints[i].X,
                                                                                                                      InternalModulePoints[i].Y,
                                                                                                                      InternalModulePoints[i].Z);

                    workPoint.SetFixed(InternalModulePoints[i].ToPoint());
                }

                else
                {
                    workPoint = layoutComponentDefinition.WorkPoints.AddFixed(InternalModulePoints[i].ToPoint(), false);
                    PointObjects[i].Binder.SetObjectForTrace <WorkPoint>(workPoint, ModuleUtilities.ReferenceKeysSorter);
                }

                //workPoint.Visible = false;

                object workPointProxyObject;
                layoutOccurrence.CreateGeometryProxy(workPoint, out workPointProxyObject);
                LayoutWorkPointProxies.Add((WorkPointProxy)workPointProxyObject);
                LayoutWorkPoints.Add(workPoint);
            }

            //If we will have more than 2 constraints, it will help assembly stability later
            //if we have a plane to constrain to first.
            if (InternalModulePoints.Count > 2)
            {
                WorkPlane workPlane;
                //TODO: Is this a good idea? Why is this a list? Will we ever have more
                //than work plane?
                PlaneObjects[0].Binder.ContextManager.BindingContextManager = refKeyManager;
                if (PlaneObjects[0].Binder.GetObjectFromTrace <WorkPlane>(out workPlane))
                {
                    if (workPlane.DefinitionType == WorkPlaneDefinitionEnum.kThreePointsWorkPlane)
                    {
                        workPlane.SetByThreePoints(LayoutWorkPoints[0], LayoutWorkPoints[1], LayoutWorkPoints[2]);
                        LayoutWorkPlane = workPlane;
                        object wPlaneProxyObject;
                        layoutOccurrence.CreateGeometryProxy(workPlane, out wPlaneProxyObject);
                        LayoutWorkPlaneProxy = (WorkPlaneProxy)wPlaneProxyObject;
                    }
                }
                else
                {
                    //If the first three points are colinear, adding a workplane will fail.  We will check the area of a triangle
                    //described by the first three points. If the area is very close to 0, we can assume these points are colinear, and we should
                    //not attempt to construct a work plane from them.
                    Inventor.Point pt1 = LayoutWorkPoints[0].Point;
                    Inventor.Point pt2 = LayoutWorkPoints[1].Point;
                    Inventor.Point pt3 = LayoutWorkPoints[2].Point;
                    if (Math.Abs(pt1.X * (pt2.Y - pt3.Y) + pt2.X * (pt3.Y - pt1.Y) + pt3.X * (pt1.Y - pt2.Y)) > .0000001)
                    {
                        workPlane = layoutComponentDefinition.WorkPlanes.AddByThreePoints(LayoutWorkPoints[0], LayoutWorkPoints[1], LayoutWorkPoints[2], false);
                        PlaneObjects[0].Binder.SetObjectForTrace <WorkPlane>(workPlane, ModuleUtilities.ReferenceKeysSorter);
                        workPlane.Grounded = true;
                        //workPlane.Visible = false;
                        LayoutWorkPlane = workPlane;
                        object wPlaneProxyObject;
                        layoutOccurrence.CreateGeometryProxy(workPlane, out wPlaneProxyObject);
                        LayoutWorkPlaneProxy = (WorkPlaneProxy)wPlaneProxyObject;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        internal void PlaceModule()
        {
            ComponentOccurrence topOccurrence;

            if (AssemblyOccurrenceObject.Binder.GetObjectFromTrace <ComponentOccurrence>(out topOccurrence))
            {
                topOccurrence.Adaptive = true;
                PersistenceManager.ActiveAssemblyDoc.Update2(true);
                topOccurrence.Adaptive = false;
                if (topOccurrence.DefinitionDocumentType == DocumentTypeEnum.kAssemblyDocumentObject)
                {
                    var doc = (AssemblyDocument)topOccurrence.Definition.Document;
                    doc.Save2();
                }
                else if (topOccurrence.DefinitionDocumentType == DocumentTypeEnum.kPartDocumentObject)
                {
                    var doc = (PartDocument)topOccurrence.Definition.Document;
                    doc.Save2();
                }
            }

            else
            {
                AssemblyComponentDefinition assemblyCompDef = PersistenceManager.ActiveAssemblyDoc.ComponentDefinition;
                topOccurrence = assemblyCompDef.Occurrences.Add(ModuleAssemblyPath, TransformationMatrix);
                ComponentOccurrencesEnumerator topOccSubs = topOccurrence.SubOccurrences;
                int topOccSubsCount           = topOccSubs.Count;
                ComponentOccurrence layoutOcc = null;
                ComponentOccurrence frameOcc  = null;
                for (int i = 0; i < topOccSubsCount; i++)
                {
                    ComponentOccurrence currentSub = topOccSubs[i + 1];
                    //This is by convention.  Possible better alternative is to have this be
                    //explicitly set in the user interface.
                    if (currentSub.Name == "Template Layout:1")
                    {
                        layoutOcc = currentSub;
                        LayoutComponentDefinition = (PartComponentDefinition)layoutOcc.Definition;
                    }

                    //What did I do this for? I think for old flea flicker parameter updating.
                    if (currentSub.Name == "Frame0001:1")
                    {
                        frameOcc = currentSub;
                        FrameComponentDefinition = (AssemblyComponentDefinition)frameOcc.Definition;
                    }

                    else
                    {
                        TopOccurrences.Add(currentSub);
                    }
                }
                int workPointCount = LayoutComponentDefinition.WorkPoints.Count;
                for (int j = 0; j < workPointCount; j++)

                //TODO  Need to put logic in that test a layout file for derivedparametertables collection.Count != 0
                //then do the copy of the layout file, get the layout file and swap out the document descriptor IN APPRENTICE.
                {
                    WorkPoint currentWP;
                    currentWP = LayoutComponentDefinition.WorkPoints[j + 1];
                    TargetWorkPoints.Add(currentWP);
                    object         currentWPProxyObject;
                    WorkPointProxy currentWPProxy;
                    layoutOcc.CreateGeometryProxy(currentWP, out currentWPProxyObject);
                    currentWPProxy = (WorkPointProxy)currentWPProxyObject;
                    TargetWorkPointProxies.Add(currentWPProxy);
                }
                //TODO Fix this to be more intellegent.  What if assembly had two planes (rooms etc.).
                WorkPlane targetWorkPlane;
                if (LayoutComponentDefinition.WorkPlanes.Count > 3)
                {
                    targetWorkPlane = (WorkPlane)LayoutComponentDefinition.WorkPlanes[4];
                    object wPlaneProxyObject;
                    layoutOcc.CreateGeometryProxy(targetWorkPlane, out wPlaneProxyObject);
                    TargetWorkPlaneProxy    = (WorkPlaneProxy)wPlaneProxyObject;
                    targetWorkPlane.Visible = false;
                }
                //Workplane constraints needed or not?
                //Make sure the target assembly's layout is adaptive.
                layoutOcc.Adaptive = true;
                PersistenceManager.ActiveAssemblyDoc.ComponentDefinition.Constraints.AddMateConstraint(TargetWorkPlaneProxy, LayoutWorkPlaneProxy, 0);

                if (FirstTime == true)
                {
                    for (int f = 0; f < PointObjects.Count; f++)
                    {
                        //TODO this is uncertain.  It changes from test to test, need to get better handle on the indexing of points.
                        PersistenceManager.ActiveAssemblyDoc
                        .ComponentDefinition
                        .Constraints
                        .AddMateConstraint(TargetWorkPointProxies[f + 1], LayoutWorkPointProxies[f], 0);
                    }

                    topOccurrence.Adaptive = true;
                    PersistenceManager.ActiveAssemblyDoc.Update2(true);
                    topOccurrence.Adaptive = false;
                    //layoutOcc.Visible = false;
                }

                else
                {
                    for (int f = 0; f < PointObjects.Count; f++)
                    {
                        //TODO this is uncertain.  It changes from test to test, need to get better handle on the indexing of points.
                        MateConstraint mateConstraint = PersistenceManager.ActiveAssemblyDoc
                                                        .ComponentDefinition
                                                        .Constraints
                                                        .AddMateConstraint(TargetWorkPointProxies[f + 1], LayoutWorkPointProxies[f], 0);
                        if (f > 0)
                        {
                            //These mate constraints will fail out in space because of double accuracy issues unless they are relaxed some.
                            mateConstraint.ConstraintLimits.MaximumEnabled     = true;
                            mateConstraint.ConstraintLimits.Maximum.Expression = ".5 in";
                        }
                    }
                    //layoutOcc.Visible = false;
                }
                AssemblyOccurrenceObject.Binder.SetObjectForTrace <ComponentOccurrence>(topOccurrence, ModuleUtilities.ReferenceKeysSorter);
                if (topOccurrence.DefinitionDocumentType == DocumentTypeEnum.kAssemblyDocumentObject)
                {
                    var doc = (AssemblyDocument)topOccurrence.Definition.Document;
                    doc.Save2();
                }
                else if (topOccurrence.DefinitionDocumentType == DocumentTypeEnum.kPartDocumentObject)
                {
                    var doc = (PartDocument)topOccurrence.Definition.Document;
                    doc.Save2();
                }
            }
        }
Ejemplo n.º 7
0
        internal void PlaceWorkGeometryForContsraints(PartComponentDefinition layoutComponentDefinition, ComponentOccurrence layoutOccurrence, int moduleNumber)
        {
            PartDocument partDoc = (PartDocument)layoutComponentDefinition.Document;
            ReferenceKeyManager refKeyManager = partDoc.ReferenceKeyManager;
            
            //TODO: Property ModuleId can be factored out and _binder.ContextData can be used instead.
            ModuleId = moduleNumber;

            for (int i = 0; i < PointObjects.Count; i++)
            {
                //Each ModuleObject needs to have its ContextManager set.
                PointObjects[i].Binder.ContextManager.BindingContextManager = refKeyManager;
                WorkPoint workPoint;
                if (PointObjects[i].Binder.GetObjectFromTrace<WorkPoint>(out workPoint))
                {
                    Inventor.Point newLocation = PersistenceManager.InventorApplication.TransientGeometry.CreatePoint(InternalModulePoints[i].X,
                                                                                                                              InternalModulePoints[i].Y,
                                                                                                                              InternalModulePoints[i].Z);

                    workPoint.SetFixed(InternalModulePoints[i].ToPoint());
                }

                else
                {
                    workPoint = layoutComponentDefinition.WorkPoints.AddFixed(InternalModulePoints[i].ToPoint(), false);
                    PointObjects[i].Binder.SetObjectForTrace<WorkPoint>(workPoint, ModuleUtilities.ReferenceKeysSorter);
                }

                //workPoint.Visible = false;

                object workPointProxyObject;
                layoutOccurrence.CreateGeometryProxy(workPoint, out workPointProxyObject);
                LayoutWorkPointProxies.Add((WorkPointProxy)workPointProxyObject);
                LayoutWorkPoints.Add(workPoint);
            }

            //If we will have more than 2 constraints, it will help assembly stability later
            //if we have a plane to constrain to first.
            if (InternalModulePoints.Count > 2)
            {
                
                WorkPlane workPlane;
                //TODO: Is this a good idea? Why is this a list? Will we ever have more
                //than work plane?  
                PlaneObjects[0].Binder.ContextManager.BindingContextManager = refKeyManager;
                if (PlaneObjects[0].Binder.GetObjectFromTrace<WorkPlane>(out workPlane))
                {
                    if (workPlane.DefinitionType == WorkPlaneDefinitionEnum.kThreePointsWorkPlane)
                    {
                        workPlane.SetByThreePoints(LayoutWorkPoints[0], LayoutWorkPoints[1], LayoutWorkPoints[2]);
                        LayoutWorkPlane = workPlane;
                        object wPlaneProxyObject;
                        layoutOccurrence.CreateGeometryProxy(workPlane, out wPlaneProxyObject);
                        LayoutWorkPlaneProxy = (WorkPlaneProxy)wPlaneProxyObject;
                    }
                }
                else
                {
                    //If the first three points are colinear, adding a workplane will fail.  We will check the area of a triangle 
                    //described by the first three points. If the area is very close to 0, we can assume these points are colinear, and we should
                    //not attempt to construct a work plane from them.
                    Inventor.Point pt1 = LayoutWorkPoints[0].Point;
                    Inventor.Point pt2 = LayoutWorkPoints[1].Point;
                    Inventor.Point pt3 = LayoutWorkPoints[2].Point;
                    if (Math.Abs(pt1.X * (pt2.Y - pt3.Y) + pt2.X * (pt3.Y - pt1.Y) + pt3.X * (pt1.Y - pt2.Y)) > .0000001)
                    {
                        workPlane = layoutComponentDefinition.WorkPlanes.AddByThreePoints(LayoutWorkPoints[0], LayoutWorkPoints[1], LayoutWorkPoints[2], false);
                        PlaneObjects[0].Binder.SetObjectForTrace<WorkPlane>(workPlane, ModuleUtilities.ReferenceKeysSorter);
                        workPlane.Grounded = true;
                        //workPlane.Visible = false;
                        LayoutWorkPlane = workPlane;
                        object wPlaneProxyObject;
                        layoutOccurrence.CreateGeometryProxy(workPlane, out wPlaneProxyObject);
                        LayoutWorkPlaneProxy = (WorkPlaneProxy)wPlaneProxyObject;
                    }
                }
            }
        }
Ejemplo n.º 8
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.Hide();
                double h = 1, w = 1;
                if (m_Asm != null)
                {
                    CommandManager cmdMgr = invApp.CommandManager;
                    string         txt    = "";
                    //input = cmdMgr.UserInputEvents;
                    intEvts = cmdMgr.CreateInteractionEvents();
                    intEvts.InteractionDisabled = false;
                    sel = intEvts.SelectEvents;
                    sel.AddSelectionFilter(SelectionFilterEnum.kPartFaceFilter);
                    sel.OnSelect       += new SelectEventsSink_OnSelectEventHandler(select);
                    mouse               = intEvts.MouseEvents;
                    mouse.OnMouseClick += new MouseEventsSink_OnMouseClickEventHandler(selPt);
                    intEvts.Start();
                    intEvts.StatusBarText = "Выберите поверхность:";
                    //input.OnSelect += new UserInputEventsSink_OnSelectEventHandler(select);
                    //face = (Face)cmdMgr.Pick(SelectionFilterEnum.kPartFaceFilter, "Выберите поверхность:");
                    //input.OnSelect -= new UserInputEventsSink_OnSelectEventHandler(select);
                    flag = true;
                    while (flag)
                    {
                        invApp.UserInterfaceManager.DoEvents();
                    }
                    intEvts.Stop();
                    sel.OnSelect -= new SelectEventsSink_OnSelectEventHandler(select);
                    intEvts.MouseEvents.OnMouseClick -= new MouseEventsSink_OnMouseClickEventHandler(selPt);
                    List <string> tmp  = new List <string>();
                    string        name = comboBox1.Text;
                    m_Doc = (PartDocument)invApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject, CreateVisible: false);
                    m_Doc.ComponentDefinition.BOMStructure = BOMStructureEnum.kPurchasedBOMStructure;
                    m_Doc.UnitsOfMeasure.LengthUnits       = UnitsTypeEnum.kMillimeterLengthUnits;
                    string filename = "";
                    for (int i = 0; i < attrXML.Count; i++)
                    {
                        if (attrXML[i].StartsWith("Name=" + name))
                        {
                            tmp.Add(attrXML[i]);
                            tmp.Add(valXML[i]);
                            i++;
                            while (i < attrXML.Count && attrXML[i].StartsWith("RowHeight="))
                            {
                                tmp.Add(attrXML[i]);
                                tmp.Add(valXML[i]);
                                i++;
                            }
                            break;
                        }
                    }
                    string nameStricker = "";
                    string strTmp       = tbl.substring(tmp[0], "PartNumber=");
                    nameStricker = strTmp;
                    if (strTmp != "")
                    {
                        m_Doc.PropertySets[3][2].Value = strTmp; filename += strTmp;
                    }
                    strTmp = tbl.substring(tmp[0], "Description=");
                    if (strTmp != "")
                    {
                        m_Doc.PropertySets[3][14].Value = strTmp;
                        filename += (filename == "") ? strTmp : "-" + strTmp;
                    }

                    Func <string, double> conv = s =>
                    {
                        char rep = (separator == ',') ? '.' : ',';
                        return((s.IndexOf(separator) != -1) ? Convert.ToDouble(s) : Convert.ToDouble(s.Replace(rep, separator)));
                    };

                    strTmp = tbl.substring(tmp[0], "Height=");
                    if (strTmp != "")
                    {
                        h = conv(strTmp);
                    }
                    strTmp = tbl.substring(tmp[0], "Width=");
                    if (strTmp != "")
                    {
                        w = conv(strTmp);
                    }
                    strTmp = tbl.substring(tmp[0], "txt=");
                    if (strTmp != "")
                    {
                        txt = strTmp;
                    }
                    strTmp = tbl.substring(tmp[0], "Sort=");
                    if (strTmp != "")
                    {
                        m_Doc.PropertySets[1][4].Value = strTmp;
                    }
                    strTmp = tbl.substring(tmp[0], "Note=");
                    if (strTmp != "")
                    {
                        m_Doc.PropertySets[1][5].Value = strTmp;
                    }
                    strTmp = tbl.substring(tmp[0], "Triangle=");
                    if (strTmp == "False")
                    {
                        triangle = false;
                    }
                    else
                    {
                        triangle = true;
                    }
                    strTmp = tbl.substring(tmp[0], "Offset1=");
                    if (strTmp != "")
                    {
                        offset1 = conv(strTmp) / 10;
                    }
                    strTmp = tbl.substring(tmp[0], "Offset2=");
                    if (strTmp != "")
                    {
                        offset2 = conv(strTmp) / 10;
                    }
                    strTmp = tbl.substring(tmp[0], "Mirror=");
                    if (strTmp != "")
                    {
                        mirror = true;
                    }

                    if (face.SurfaceType == SurfaceTypeEnum.kPlaneSurface)
                    {
                        filename += "-" + w.ToString() + "x" + h.ToString();
                        if (!System.IO.File.Exists(path(m_Asm.FullFileName) + filename + ".ipt"))
                        {
                            Plane plane = (Plane)face.Geometry;
                            vecZ = plane.Normal;
                            if (!triangle)
                            {
                                planarSticker(ref m_Doc, h, w, txt);
                            }
                            else
                            {
                                triangleSticker(ref m_Doc, h, w, txt);
                            }
                        }
                    }
                    else if (face.SurfaceType == SurfaceTypeEnum.kCylinderSurface)
                    {
                        R         = Math.Round(((Cylinder)face.Geometry).Radius, 2);
                        filename += "-" + w.ToString() + "x" + h.ToString() + "x" + (R * 10).ToString();
                        if (!System.IO.File.Exists(path(m_Asm.FullFileName) + filename + ".ipt"))
                        {
                            if (this.radioButton1.Checked)
                            {
                                circleSticker(ref m_Doc, w, h, R, txt); filename += "(верт)";
                            }
                            else
                            {
                                circleSticker(ref m_Doc, h, w, R, txt);
                            }
                        }
                    }
                    filename = path(m_Asm.FullFileName) + filename + ".ipt";
                    if (!System.IO.File.Exists(filename))
                    {
                        m_Doc.SaveAs(filename, false);
                        Document tmp_Doc = invApp.Documents.Open(filename, false);
                        tmp_Doc.PropertySets[3][2].Value = nameStricker;
                        tmp_Doc.Save();
                        tmp_Doc.Close();
                        m_Doc.Close();
                    }

                    Inventor.Point pt = m_TG.CreatePoint();
                    //pos = m_TG.CreateMatrix();


                    //rot.SetCoordinateSystem(insPt, m_TG.CreateVector(YCoord: 1), m_TG.CreateVector(1) , m_TG.CreateVector(ZCoord: 1));

                    //pos.TransformBy(rot);
//                         if (face.SurfaceType == SurfaceTypeEnum.kPlaneSurface && !System.IO.File.Exists(filename))
//                         {

                    //}

                    AssemblyComponentDefinition compDef = m_Asm.ComponentDefinition;
                    //pos.Cell[4, 1] = insPt.X; pos.Cell[4, 2] = insPt.Y; pos.Cell[4, 3] = insPt.Z;
                    rot = m_TG.CreateMatrix();
                    ComponentOccurrence occ = compDef.Occurrences.Add(filename, rot);
                    Vector vec1             = InvDoc.u.getAxis(face);
                    Vector vec2             = InvDoc.u.getAxis(occ);
                    if (!InvDoc.u.eq(vec1, vec2))
                    {
                        rot.SetToRotateTo(vec1, vec2);
                    }
                    //m_Asm.ComponentDefinition.WorkPoints.AddFixed(insPt);
                    vec = pt.VectorTo(insPt);
                    rot.SetTranslation(vec);
                    //rot.Cell[1, 4] = insPt.X; rot.Cell[2, 4] = insPt.Y; rot.Cell[3, 4] = insPt.Z;
                    //occ.Transformation = rot;
                    occ.SetTransformWithoutConstraints(rot);
                    //rot.SetToRotateTo(m_TG.CreateVector(ZCoord: 1), vecZ.AsVector(), m_TG.CreateVector(XCoord: 1));
                    //rot.SetTranslation(vec);
                    //return;
                    //Face face2 = occ.SurfaceBodies[1].Faces[5];
                    object fac = null;
                    occ.CreateGeometryProxy(((PartComponentDefinition)occ.Definition).WorkPlanes[3], out fac);
                    WorkPlaneProxy          face2 = (WorkPlaneProxy)fac;
                    PartComponentDefinition def   = (PartComponentDefinition)occ.Definition;
                    //Inventor.Point pt2 = def.WorkPoints[1].Point;
                    //pos.Invert();
                    //pt2.TransformBy(pos);
                    if (face.SurfaceType == SurfaceTypeEnum.kPlaneSurface)
                    {
                        compDef.Constraints.AddFlushConstraint(face, face2, 0);
                    }
                    else if (face.SurfaceType == SurfaceTypeEnum.kCylinderSurface)
                    {
                        //foreach (Face f in occ.SurfaceBodies[1].Faces)
                        //{
                        //    if (f.SurfaceType == SurfaceTypeEnum.kCylinderSurface)
                        //    {
                        //        if (Math.Round(((Cylinder)f.Geometry).Radius, 2) == R)
                        //        {
                        //            face2 = f; break;
                        //        }
                        //    }
                        //}
                        occ.CreateGeometryProxy(((PartComponentDefinition)occ.Definition).WorkAxes[2], out fac);
                        compDef.Constraints.AddMateConstraint((WorkAxisProxy)fac, face, 0, InferredTypeEnum.kInferredLine, InferredTypeEnum.kInferredLine);
                    }
                    //Matrix trans = m_TG.CreateMatrix();
                    //trans.SetTranslation(vec,true);
                    //occ.Transformation.Cell[1, 4] = insPt.X; occ.Transformation.Cell[2, 4] = insPt.Y; occ.Transformation.Cell[3, 4] = insPt.Z;

                    object face1 = null;

                    if (face.SurfaceType == SurfaceTypeEnum.kCylinderSurface)
                    {
                        if (offset1 > 0.05 && offset1 < 0.4)
                        {
                            //PartComponentDefinition pDef = ((PartDocument)((FaceProxy)face).ContainingOccurrence.Definition.Document).ComponentDefinition;
                            ((FaceProxy)face).ContainingOccurrence.CreateGeometryProxy(InvDoc.u.getPlane((FaceProxy)face), out fac);
                            //createProxy((FaceProxy)face, (int)(offset1 * 10), ref fac);
                            //occ.CreateGeometryProxy(((PartComponentDefinition)occ.Definition).WorkPlanes[(int)(offset1 * 10)], out fac);
                            face1   = fac;
                            offset1 = 0;
                        }
                        else /*if (offset1 != 0)*/
                        {
                            offset1 += w / 20;
                            ent      = (Edge)cmdMgr.Pick(SelectionFilterEnum.kPartEdgeCircularFilter, "Выберите торцевое ребро:");
                            if (ent.Faces[1].SurfaceType == SurfaceTypeEnum.kPlaneSurface)
                            {
                                face1 = ent.Faces[1];
                            }
                            else
                            {
                                face1 = ent.Faces[2];
                            }
                        }
//                         if (offset2 != 0)
//                         {
//                             offset2 += h / 20;
//                         }
                        //face1 = cmdMgr.Pick(SelectionFilterEnum.kPartEdgeCircularFilter, "Выберите тор"
                    }
                    else
                    {
                        if (offset1 > 0.05 && offset1 < 0.4)
                        {
                            createProxy((FaceProxy)face, (int)(offset1 * 10), ref fac);
                            //occ.CreateGeometryProxy(((PartComponentDefinition)occ.Definition).WorkPlanes[(int)(offset1 * 10)], out fac);
                            edge1   = fac;
                            offset1 = 0;
                        }
                        else /*if (offset1 != 0)*/
                        {
                            offset1 += h / 20;
                            edge1    = cmdMgr.Pick(SelectionFilterEnum.kAllEntitiesFilter, "Выберите ребро или плоскость:");
                        }
                    }
                    //face2 = occ.SurfaceBodies[1].Faces[2];
                    occ.CreateGeometryProxy(((PartComponentDefinition)occ.Definition).WorkPlanes[2], out fac);
                    face2 = (WorkPlaneProxy)fac;
                    MateConstraint mc = null; FlushConstraint fc = null;

//                     if (offset2 < 0.4)
//                     {
//                         face1 = createProxy(occ, (int)(offset2 * 10)); //compDef.WorkPlanes[(int)(offset2*10)];
//                     }
                    //else if (offset2 != 0) offset2 += h / 20;
                    if (face.SurfaceType == SurfaceTypeEnum.kCylinderSurface)
                    {
                        fc = compDef.Constraints.AddFlushConstraint(face2, face1, offset1);
                    }
                    else
                    {
                        mc = compDef.Constraints.AddMateConstraint(face2, edge1, offset1);
                    }
//                     Inventor.Point ptmc = occ.RangeBox.MinPoint;
//                     SelectionFilterEnum [] f = {SelectionFilterEnum.kPartFaceFilter};
//                     ObjectsEnumerator en = compDef.FindUsingPoint(ptmc, ref f, 0.1);
                    if (/*en.Count < 3*/ InvDoc.u.intersPoint(compDef, occ, (FaceProxy)face))
                    {
                        if (mc != null)
                        {
                            mc.Offset.Value = -(double)mc.Offset.Value;
                        }
                        //else fc.Offset.Value = - (double)fc.Offset.Value;
                    }
                    if (offset2 > 0.05 && offset2 < 0.4)
                    {
                        createProxy((FaceProxy)face, (int)(offset2 * 10), ref fac);
                        //occ.CreateGeometryProxy(((PartComponentDefinition)face.ReferenceComponent.ReferencedDocumentDescriptor.ReferencedDocument.Definition).WorkPlanes[(int)(offset2 * 10)], out fac);
                        edge2   = fac;
                        offset2 = 0;
                    }
                    else /*if (offset2 != 0)*/
                    {
                        if (face.SurfaceType == SurfaceTypeEnum.kCylinderSurface)
                        {
                            offset2 += h / 20;
                        }
                        else
                        {
                            offset2 += w / 20;
                        }
                        edge2 = cmdMgr.Pick(SelectionFilterEnum.kAllEntitiesFilter, "Выберите ребро или плоскость:");
                    }

                    //face2 = occ.SurfaceBodies[1].Faces[1];
                    occ.CreateGeometryProxy(((PartComponentDefinition)occ.Definition).WorkPlanes[1], out fac);
                    face2 = (WorkPlaneProxy)fac;
                    mc    = compDef.Constraints.AddMateConstraint(face2, edge2, offset2);
                    if (mc.HealthStatus == HealthStatusEnum.kInconsistentHealth)
                    {
                        occ.Constraints[2].ConvertToFlushConstraint(occ.Constraints[2].EntityOne, occ.Constraints[2].EntityTwo, offset1);
                    }
                    //en = compDef.FindUsingPoint(ptmc, ref f, 0.1);
                    m_Asm.Update();
                    if (/*en.Count < 3*/ InvDoc.u.intersPoint(compDef, occ, (FaceProxy)face))
                    {
                        mc.Delete();
                        mc = compDef.Constraints.AddMateConstraint(face2, edge2, offset2);
                        m_Asm.Update();
                        if (InvDoc.u.intersPoint(compDef, occ, (FaceProxy)face))
                        {
                            //mc.Delete();
                            //mc = compDef.Constraints.AddMateConstraint(face2, edge1, -offset2);
                            double dist = Macros.StandardAddInServer.m_inventorApplication.MeasureTools.GetMinimumDistance(occ.RangeBox.MinPoint, edge2);
                            if (dist > offset2 * 2)
                            {
                                rot = m_TG.CreateMatrix();
                                Vector tr = occ.Transformation.Translation;
                                rot.SetToRotation(Math.PI, InvDoc.u.getAxis(occ), m_TG.CreatePoint(tr.X, tr.Y, tr.Z));
                                mc.Delete();
                                occ.Transformation = rot;
                                mc = compDef.Constraints.AddMateConstraint(face2, edge2, offset2);
                                if (InvDoc.u.intersPoint(compDef, occ, (FaceProxy)face))
                                {
                                    mc.Offset.Value = -(double)mc.Offset.Value;
                                }
                            }
                            else if (InvDoc.u.intersPoint(compDef, occ, (FaceProxy)face))
                            {
                                mc.Offset.Value = -(double)mc.Offset.Value;
                            }
                            m_Asm.Update();
                        }
                        //mc.Offset.Value = -(double)mc.Offset.Value;
                    }
                    //occ.Transformation = trans;
                }

                this.Close();
            }
            catch (Exception)
            {
                //MessageBox.Show(ex.ToString());
                this.Close();
            }
        }
Ejemplo n.º 9
0
        private void createProxy(FaceProxy face, int num, ref object fac)
        {
            ComponentOccurrence occ = face.ContainingOccurrence;

            occ.CreateGeometryProxy(((PartComponentDefinition)occ.Definition).WorkPlanes[num], out fac);
        }