Esempio n. 1
0
        public static Polyline ConvertToPolyline(this Polyline2d pl2d, QuickTransaction tr)
        {
            var mSpace = (BlockTableRecord)SymbolUtilityServices.GetBlockModelSpaceId(tr.Db).GetObject(OpenMode.ForWrite);

            if (pl2d.PolyType == Poly2dType.CubicSplinePoly || pl2d.PolyType == Poly2dType.QuadSplinePoly)
            {
                return(null);
            }
            var pline = new Polyline();

            pline.ConvertFrom(pl2d, false);
            mSpace.AppendEntity(pline);
            tr.AddNewlyCreatedDBObject(pline, true);
            pl2d.Erase();
            return(pline);
        }
        private void ConvertToTargetProfiles(Document doc, Polyline2d partExtens)
        {
            var pnts = partExtens.GeometricExtents;

            //删除边界
            using (Transaction trans = doc.Database.TransactionManager.StartTransaction())
            {
                partExtens.Erase();
                trans.Commit();
            }
            List <Entity>         listCurveBoundary = new List <Entity>();
            PromptSelectionResult psr = doc.Editor.SelectWindow(pnts.MaxPoint, pnts.MinPoint);

            if (psr.Status == PromptStatus.OK)
            {
                List <ObjectId> oids = new List <ObjectId>();
                foreach (SelectedObject item in psr.Value)
                {
                    using (Transaction trans = doc.Database.TransactionManager.StartTransaction())
                    {
                        Entity ent = trans.GetObject(item.ObjectId, OpenMode.ForWrite) as Entity;
                        if (ent.Layer == "Rulers") //删除无用的标尺
                        {
                            ent.Erase();
                        }
                        if (ent.Layer == "Markingline")//marking 线
                        {
                            ent.Layer      = "0";
                            ent.ColorIndex = 4;
                        }
                        if (ent is DBText && (ent.Layer == "Elevation Plane" || ent.Layer == "MarginSymbol" || ent.Layer == "ProfileSide"))//frame文字
                        {
                            ent.Layer = "0";
                            if (ent.Layer != "ProfileSide")
                            {
                                (ent as DBText).Height = 20;
                            }
                            ent.ColorIndex = 6;
                        }
                        if (ent is Spline)//存在样条曲线
                        {
                            var sp = ent as Spline;
                            sp.Erase();
                            Curve pline = sp.ToPolylineWithPrecision(1);
                            pline.Layer      = "0";
                            pline.ColorIndex = 3;
                            var oid = AddToModelSpace(doc.Database, pline);
                            oids.Add(oid);
                        }
                        if ((ent is Line || ent is Arc) && ent.Layer == "ProfileWebOrient")//存在样条曲线
                        {
                            ent.Layer      = "0";
                            ent.ColorIndex = 3;
                        }
                        trans.Commit();
                    }
                }
                if (oids.Count > 0)
                {
                    using (Transaction trans = doc.Database.TransactionManager.StartTransaction())
                    {
                        List <Polyline> allPlines = new List <Polyline>();
                        foreach (ObjectId item in oids)
                        {
                            Polyline ent = trans.GetObject(item, OpenMode.ForWrite) as Polyline;
                            allPlines.Add(ent);
                        }
                        var minY        = allPlines.Min(p => p.StartPoint.Y);
                        var targetPline = allPlines.Where(p => p.StartPoint.Y == minY).ToList()[0];
                        for (int i = allPlines.Count - 1; i >= 0; i--)
                        {
                            if (allPlines[i].ObjectId != targetPline.ObjectId)
                            {
                                allPlines[i].Erase();
                                allPlines.Remove(allPlines[i]);
                            }
                        }
                        var dbs = targetPline.GetOffsetCurves(-80);
                        foreach (DBObject item in dbs)
                        {
                            (item as Entity).Layer      = "0";
                            (item as Entity).ColorIndex = 3;
                            AddToModelSpace(doc.Database, item as Entity);
                        }
                        trans.Commit();
                    }
                }
            }
        }
        public void ConvertToTargetPart(Document doc, Polyline2d partExtens)//$DrawingExtens
        {
            var pnts = partExtens.GeometricExtents;

            using (Transaction trans = doc.Database.TransactionManager.StartTransaction())
            {
                partExtens.Erase();
                trans.Commit();
            }
            List <Entity>         listCurveBoundary = new List <Entity>();
            PromptSelectionResult psr = doc.Editor.SelectWindow(pnts.MaxPoint, pnts.MinPoint);

            if (psr.Status == PromptStatus.OK)
            {
                List <ObjectId> oids = new List <ObjectId>();
                foreach (SelectedObject item in psr.Value)
                {
                    using (Transaction trans = doc.Database.TransactionManager.StartTransaction())
                    {
                        Entity ent = trans.GetObject(item.ObjectId, OpenMode.ForWrite) as Entity;
                        if (ent is Curve && (ent.ColorIndex.ToString() == "1" || ent.ColorIndex.ToString() == "3"))
                        {
                            ent.Layer = "0"; ent.ColorIndex = 4;
                        }                                                                                                                                   //内部marking线
                        if (ent is Curve && ent.ColorIndex.ToString() == "7" && ent.Layer != "_Auto")
                        {
                            ent.Layer = "0"; ent.ColorIndex = 3; listCurveBoundary.Add(ent);
                        }                                                                                                                                                 //外轮廓线
                        if (ent is Curve && ent.Layer == "_Auto")
                        {
                            ent.Layer = "0"; ent.ColorIndex = 4;
                        }                                                                                 //内部marking线
                        if (ent is DBText)
                        {
                            ent.Layer = "0"; ent.ColorIndex = 6;
                        }                  //外轮廓线
                        if (ent is Spline) //存在样条曲线的边界线
                        {
                            var sp = ent as Spline;
                            listCurveBoundary.Remove(ent as Curve);
                            sp.Erase();
                            Curve pline = sp.ToPolylineWithPrecision(1);
                            var   oid   = AddToModelSpace(doc.Database, pline);
                            oids.Add(oid);
                        }
                        trans.Commit();
                    }
                }
                using (Transaction trans = doc.Database.TransactionManager.StartTransaction())
                {
                    Entity ent = null;
                    if (oids.Count == 0)//全是直线的情况
                    {
                        Polyline          pline  = new Polyline();
                        Point3dCollection pnts3d = new Point3dCollection();
                        for (int i = listCurveBoundary.Count - 1; i >= 0; i--)
                        {
                            Line l = listCurveBoundary[i] as Line;
                            pnts3d.Add(l.StartPoint);
                            pnts3d.Add(l.EndPoint);
                            l.Erase();
                        }
                        pline.CreatePolyline(pnts3d);
                        pline.Layer = "0";
                        AddToModelSpace(doc.Database, pline);
                    }
                    else
                    {
                        ent = oids[0].GetObject(OpenMode.ForWrite) as Entity;
                        for (int i = 1; i < oids.Count; i++)
                        {
                            listCurveBoundary.Add(oids[0].GetObject(OpenMode.ForWrite) as Entity);
                        }
                        Autodesk.AutoCAD.Geometry.IntegerCollection intSet = ent.JoinEntities(listCurveBoundary.ToArray());
                        for (int i = 0; i < listCurveBoundary.Count; i++)
                        {
                            listCurveBoundary[i].Erase();
                        }
                    }
                    trans.Commit();
                }
            }
        }
Esempio n. 4
0
        public void convertTo3d()
        {
            Database db = HostApplicationServices.WorkingDatabase;

            Autodesk.AutoCAD.DatabaseServices.TransactionManager myTm = db.TransactionManager;
            Transaction myT = db.TransactionManager.StartTransaction();

            //alle Blöcke wählen
            myAutoCAD.Blöcke.Instance.init();
            myAutoCAD.Blöcke.Instance.selectAll();
            Messpunkt[] vMP = Blöcke.Instance.getMP;

            //Linien

            using (DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument())
            {
                foreach (KeyValuePair <ObjectId, Line> valPair in m_LineCollection)
                {
                    ObjectId id      = valPair.Key;
                    Line     objLine = valPair.Value;

                    Messpunkt MP      = new Messpunkt();
                    Point2d   startPt = new Point2d(objLine.StartPoint.X, objLine.StartPoint.Y);
                    Point2d   endPt   = new Point2d(objLine.EndPoint.X, objLine.EndPoint.Y);

                    Line line = (Line)myT.GetObject(id, OpenMode.ForWrite);
                    if (Blöcke.Instance.findPos(ref MP, startPt, 0.001) == ErrorStatus.OK)
                    {
                        line.StartPoint = MP.Position;
                    }
                    else
                    {
                        m_lsMP_Error.Add(new Messpunkt("", startPt.X, startPt.Y, null, null, 0));
                    }

                    if (Blöcke.Instance.findPos(ref MP, endPt, 0.001) == ErrorStatus.OK)
                    {
                        line.EndPoint = MP.Position;
                    }
                    else
                    {
                        m_lsMP_Error.Add(new Messpunkt("", endPt.X, endPt.Y, null, null, 0));
                    }
                }
            }

            //Polylinien
            using (DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument())
            {
                foreach (KeyValuePair <ObjectId, Polyline> valPair in m_PolylineCollection)
                {
                    ObjectId id    = valPair.Key;
                    Polyline objPL = valPair.Value;

                    List <Point3d> lsPt3d = new List <Point3d>();

                    //Vertices iterieren
                    for (int i = 0; i < objPL.NumberOfVertices; i++)
                    {
                        Point2d   pt = objPL.GetPoint2dAt(i);
                        Messpunkt MP = new Messpunkt();

                        if (Blöcke.Instance.findPos(ref MP, pt, 0.001) == ErrorStatus.OK)
                        {
                            lsPt3d.Add(MP.Position);
                        }
                        else
                        {
                            Point3d pt3d = new Point3d(pt.X, pt.Y, 0);
                            MP.Position = pt3d;
                            m_lsMP_Error.Add(MP);
                            lsPt3d.Add(pt3d);
                        }
                    }

                    //3dPolylinie erstellen
                    //Punktliste erzeugen
                    Point3d[] vPT3d = new Point3d[lsPt3d.Count];
                    for (int i = 0; i < lsPt3d.Count; i++)
                    {
                        vPT3d[i] = lsPt3d[i];
                    }

                    //3d Polylinie erzeugen
                    Point3dCollection pt3dCol = new Point3dCollection(vPT3d);
                    Polyline3d        objPL3d = new Polyline3d(Poly3dType.SimplePoly, pt3dCol, objPL.Closed);
                    objPL3d.Layer = objPL.Layer;

                    //2d Polylinie löschen
                    Polyline pl = (Polyline)myT.GetObject(id, OpenMode.ForWrite);
                    pl.Erase(true);

                    //Polylinie in DB einfügen
                    try
                    {
                        BlockTable       bt  = (BlockTable)myT.GetObject(db.BlockTableId, OpenMode.ForRead, false);
                        BlockTableRecord btr = (BlockTableRecord)myT.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);

                        ObjectId idPL3d = btr.AppendEntity(objPL3d);
                        myT.AddNewlyCreatedDBObject(objPL3d, true);
                        objPL3d.Draw();
                    }
                    catch { }
                }
            }

            //Polyline2d
            using (DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument())
            {
                foreach (KeyValuePair <ObjectId, Polyline2d> valPair in m_Polyline2dCollection)
                {
                    ObjectId       id      = valPair.Key;
                    Polyline2d     objPL2d = valPair.Value;
                    List <Point3d> lsPt3d  = new List <Point3d>();

                    //Vertices iterieren
                    foreach (ObjectId idVertex in objPL2d)
                    {
                        Vertex2d  vertex2d = (Vertex2d)myT.GetObject(idVertex, OpenMode.ForRead);
                        Point2d   pt       = new Point2d(vertex2d.Position.X, vertex2d.Position.Y);
                        Messpunkt MP       = new Messpunkt();

                        if (Blöcke.Instance.findPos(ref MP, pt, 0.001) == ErrorStatus.OK)
                        {
                            lsPt3d.Add(MP.Position);
                        }
                        else
                        {
                            Point3d pt3d = new Point3d(pt.X, pt.Y, 0);
                            MP.Position = pt3d;
                            m_lsMP_Error.Add(MP);
                            lsPt3d.Add(pt3d);
                        }
                    }

                    //3dPolylinie erstellen
                    //Punktliste erzeugen
                    Point3d[] vPT3d = new Point3d[lsPt3d.Count];
                    for (int i = 0; i < lsPt3d.Count; i++)
                    {
                        vPT3d[i] = lsPt3d[i];
                    }

                    //3d Polylinie erzeugen
                    Point3dCollection pt3dCol = new Point3dCollection(vPT3d);
                    Polyline3d        objPL3d = new Polyline3d(Poly3dType.SimplePoly, pt3dCol, objPL2d.Closed);
                    objPL3d.Layer = objPL2d.Layer;

                    //2d Polylinie löschen
                    Polyline2d pl2d = (Polyline2d)myT.GetObject(id, OpenMode.ForWrite);
                    pl2d.Erase(true);

                    //Polylinie in DB einfügen
                    try
                    {
                        BlockTable       bt  = (BlockTable)myT.GetObject(db.BlockTableId, OpenMode.ForRead, false);
                        BlockTableRecord btr = (BlockTableRecord)myT.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);

                        ObjectId idPL3d = btr.AppendEntity(objPL3d);
                        myT.AddNewlyCreatedDBObject(objPL3d, true);
                        objPL3d.Draw();
                    }
                    catch { }
                }
            }

            //Fehler anzeigen
            if (m_lsMP_Error.Count > 0)
            {
                foreach (Messpunkt MP in m_lsMP_Error)
                {
                    MP.mark(0.2);
                }

                System.Windows.Forms.MessageBox.Show(m_lsMP_Error.Count.ToString() + " Fehler gefunden!");
            }

            else
            {
                System.Windows.Forms.MessageBox.Show("3d Konvertierung fehlerfrei durchgeführt!");
            }

            myT.Commit();
        }