예제 #1
0
        }     // end CheckLayers()

        // Convert2DPolyToLWPoly: Convert to "LWPOLYLINE" for performance reasons
        public static Polyline Convert2DPolyToLWPoly(Entity ent)
        {
            // Select the currently open AutoCAD drawing and...
            Document doc = Application.DocumentManager.MdiActiveDocument;
            // Open the Drawing Database of Objects in the current drawing
            Database db = doc.Database;

            try
            {
                Polyline poly = new Polyline();
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    // Open the Block table for read
                    BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                    // Open the Block table record Model space for write
                    BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace],
                                                        OpenMode.ForWrite) as BlockTableRecord;

                    Polyline2d poly2d = (Polyline2d)ent;
                    poly2d.UpgradeOpen();
                    poly.ConvertFrom(poly2d, false);

                    btr.AppendEntity(poly);
                    tr.AddNewlyCreatedDBObject(poly, true);

                    tr.Commit();
                } // End Transaction
                return(poly);
            }
            catch
            {
                throw new ApplicationException("2D Polyline to LWPolyline conversion failure");
            }
        } // End Convert2DPolyToLWPoly()
예제 #2
0
        public static Polyline GetProjectedPolyline(this Polyline2d pline, [NotNull] Plane plane, Vector3d direction)
        {
            var tol = new Tolerance(1e-9, 1e-9);

            if (plane.Normal.IsPerpendicularTo(direction, tol))
            {
                return(null);
            }

            if (pline.Normal.IsPerpendicularTo(direction, tol))
            {
                var dirPlane = new Plane(Point3d.Origin, direction);
                if (!pline.IsWriteEnabled)
                {
                    // ReSharper disable once UpgradeOpen
                    pline.UpgradeOpen();
                }

                pline.TransformBy(Matrix3d.WorldToPlane(dirPlane));
                var extents = pline.GeometricExtents;
                pline.TransformBy(Matrix3d.PlaneToWorld(dirPlane));
                return(GeomExt.ProjectExtents(extents, plane, direction, dirPlane));
            }

            return(GeomExt.ProjectPolyline(pline, plane, direction));
        }
예제 #3
0
        public void MyCommand() // This method can have any name
        {
            // Put your command code here
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db  = doc.Database;
            Editor   ed  = doc.Editor;

            using (Transaction Trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    TypedValue[] Filter = new TypedValue[]
                    {
                        new TypedValue((int)DxfCode.Operator, "<and"),
                        new TypedValue((int)DxfCode.LayoutName, "Model"),
                        new TypedValue((int)DxfCode.Operator, "<or"),
                        new TypedValue((int)DxfCode.Start, "POLYLINE"),
                        new TypedValue((int)DxfCode.Start, "POLYLINE3D"),
                        new TypedValue((int)DxfCode.Start, "POLYLINE2D"),
                        new TypedValue((int)DxfCode.Start, "LWPOLYLINE"),
                        new TypedValue((int)DxfCode.Operator, "or>"),
                        new TypedValue((int)DxfCode.Operator, "and>"),
                    };
                    PromptSelectionResult selresult = ed.SelectAll(new SelectionFilter(Filter));
                    ObjectId[]            Ids;
                    if (selresult.Status == PromptStatus.OK)
                    {
                        Ids = selresult.Value.GetObjectIds();
                    }
                    else
                    {
                        ed.WriteMessage("\n未找到任何多段线!");
                        return;
                    }
                    #region testcode

                    ed.WriteMessage("\n搜索到>{0}<个对象!", selresult.Value.Count);
                    #endregion

                    int counter = 0;
                    foreach (ObjectId Id in Ids)
                    {
                        Curve cur = Trans.GetObject(Id, OpenMode.ForRead) as Curve;
                        switch (cur.GetType().Name)
                        {
                        case "Polyline":
                        {
                            Polyline PL = cur as Polyline;
                            if (PL.Length == 0)
                            {
                                break;
                            }
                            if (PL.HasWidth)
                            {
                                PL.UpgradeOpen();
                                for (int i = 0; i < PL.NumberOfVertices; i++)
                                {
                                    PL.SetStartWidthAt(i, 0);
                                    PL.SetEndWidthAt(i, 0);
                                }
                                counter++;
                            }
                            break;
                        }

                        case "Polyline2d":
                        {
                            Polyline2d PL2d = cur as Polyline2d;
                            if (PL2d.Length == 0)
                            {
                                break;
                            }
                            if (PL2d.DefaultEndWidth != 0 || PL2d.DefaultStartWidth != 0)
                            {
                                PL2d.UpgradeOpen();
                                PL2d.ConstantWidth = 0;
                                counter++;
                            }
                            break;
                        }
                        }
                    }
                    ed.WriteMessage("\n共将{0}条多段线的全局宽度调整为0.", counter);

                    Trans.Commit();
                }
                catch (Autodesk.AutoCAD.Runtime.Exception Ex)
                {
                    ed.WriteMessage("出错啦!{0}", Ex.ToString());
                }
                finally
                {
                    Trans.Dispose();
                }
            }
        }