Пример #1
0
        public static Polyline LineToPoly(this QuickTransaction tr, Line line)
        {
            if (tr == null)
            {
                throw new ArgumentNullException(nameof(tr));
            }
            if (line == null)
            {
                throw new ArgumentNullException(nameof(line));
            }
            var btr = tr.BlockTableRecordCurrentSpace;

            line = tr.EnsureWritable(line);

            if (!line.IsWriteEnabled)
            {
                line = (Line)tr.GetObject(line.ObjectId, OpenMode.ForWrite);
            }
            var poly = new Polyline();

            poly.AddVertexAt(0, new Point2d(line.StartPoint.X, line.StartPoint.Y), 0, 0, 0);
            poly.AddVertexAt(1, new Point2d(line.EndPoint.X, line.EndPoint.Y), 0, 0, 0);
            poly.LayerId = line.LayerId;
            btr.AppendEntity(poly);
            tr.AddNewlyCreatedDBObject(poly, true);
            line.Erase();
            return(poly);
        }
Пример #2
0
        public static void ArcToPoly(this QuickTransaction tr, PromptSelectionResult psRes = null)
        {
            var psOpts = new PromptSelectionOptions();

            psOpts.MessageForAdding  = "\nSelect arcs to convert: ";
            psOpts.MessageForRemoval = "\n...Remove arcs: ";
            TypedValue[] filter   = { new TypedValue(0, "ARC") };
            var          ssfilter = new SelectionFilter(filter);

            psRes = (psRes?.Status == PromptStatus.OK ? psRes : null) ?? tr.GetSelection(psOpts, ssfilter);
            if (psRes.Status != PromptStatus.OK)
            {
                return;
            }
            foreach (var id in psRes.Value.GetObjectIds())
            {
                var arc  = (Arc)tr.GetObject(id, OpenMode.ForWrite);
                var poly = new Polyline();
                poly.AddVertexAt(0, new Point2d(arc.StartPoint.X, arc.StartPoint.Y), arc.GetArcBulge(), 0, 0);
                poly.AddVertexAt(1, new Point2d(arc.EndPoint.X, arc.EndPoint.Y), 0, 0, 0);
                poly.LayerId = arc.LayerId;
                tr.BlockTableRecordCurrentSpace.AppendEntity(poly);
                tr.AddNewlyCreatedDBObject(poly, true);
                arc.Erase();
            }
        }
Пример #3
0
        public static void ApplyAttributes(Database db, QuickTransaction tr, BlockReference bref)
        {
            if (bref == null)
            {
                return;
            }
            var _brec = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead);
            var btrec = _brec as BlockTableRecord;

            if (btrec == null)
            {
                return;
            }
            if (btrec.HasAttributeDefinitions)
            {
                var atcoll = bref.AttributeCollection;
                foreach (var subid in btrec)
                {
                    var ent    = (Entity)subid.GetObject(OpenMode.ForRead);
                    var attDef = ent as AttributeDefinition;
                    if (attDef != null)
                    {
                        var attRef = new AttributeReference();
                        attRef.SetDatabaseDefaults(); //optional
                        attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);
                        attRef.Position = attDef.Position.TransformBy(bref.BlockTransform);
                        attRef.Tag      = attDef.Tag;
                        attRef.AdjustAlignment(db);
                        atcoll.AppendAttribute(attRef);
                        tr.AddNewlyCreatedDBObject(attRef, true);
                    }
                }
            }
        }
Пример #4
0
        public static Polyline SplineToPoly(this QuickTransaction tr, Spline c)
        {
            var p = c.ToPolyline();

            tr.BlockTableRecordCurrentSpace.AppendEntity(p);
            tr.AddNewlyCreatedDBObject(p, true);
            c.Erase();
            return((Polyline)p);
        }
Пример #5
0
        public static Entity[] BreakOnPoint(this Curve ent, Point2d at, QuickTransaction tr, bool commitAtEnd = false)
        {
            if (ent == null)
            {
                throw new ArgumentNullException(nameof(ent));
            }
            if (tr == null)
            {
                throw new ArgumentNullException(nameof(tr));
            }
            try {
                var     btr = (BlockTableRecord)tr.GetObject(tr.Db.CurrentSpaceId, OpenMode.ForWrite);
                Point3d att = at.ToPoint3D();
                att.TransformBy(tr.Editor.CurrentUserCoordinateSystem.Inverse());
                // Check that the point is on the curve
                att = ent.GetClosestPointTo(att, false);
                var breakPoints = new Point3dCollection();
                var newCurves   = new DBObjectCollection();
                // Get the segments according to the trim points
                breakPoints.Add(att);
                newCurves = ent.GetSplitCurves(breakPoints);
                if (newCurves == null)
                {
                    tr.Editor.WriteMessage("\nGetSplitCurves failed :  Error");
                    return(new Entity[0]);
                }
                var ret = new List <Entity>();

                // Here we add the segments to the database with different colors
                for (var i = 0; i < newCurves.Count; i++)
                {
                    var pent = (Entity)newCurves[i];
                    pent.SetPropertiesFrom(ent);
                    btr.AppendEntity(pent);
                    tr.AddNewlyCreatedDBObject(pent, true);
                    ret.Add(pent);
                }

                ent.UpgradeOpen();
                ent.Erase();
                if (commitAtEnd)
                {
                    tr.Commit();
                }
                return(ret.ToArray());
            } catch (Exception ex) {
                Autodesk.AutoCAD.ApplicationServices.Core.Application.ShowAlertDialog(ex.Message + "\n" + ex.StackTrace);
                return(new Entity[0]);
            } finally { }
        }
Пример #6
0
        public static Polyline ArcToPoly(this QuickTransaction tr, Arc arc)
        {
            var btr = tr.BlockTableRecordCurrentSpace;

            arc = tr.EnsureWritable(arc);
            var poly = new Polyline();

            poly.AddVertexAt(0, new Point2d(arc.StartPoint.X, arc.StartPoint.Y), arc.GetArcBulge(), 0, 0);
            poly.AddVertexAt(1, new Point2d(arc.EndPoint.X, arc.EndPoint.Y), 0, 0, 0);
            poly.LayerId = arc.LayerId;
            btr.AppendEntity(poly);
            tr.AddNewlyCreatedDBObject(poly, true);
            arc.Erase();
            return(poly);
        }
Пример #7
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);
        }
Пример #8
0
        public static Polyline CircleToPoly(this QuickTransaction tr, Circle c)
        {
            var r = c.Radius;

            new Ellipse();
            var top       = c.Center.Add(new Vector3d(0, r, 0));
            var bottom    = c.Center.Add(new Vector3d(0, -r, 0));
            var right     = c.Center.Add(new Vector3d(r, 0, 0));
            var left      = c.Center.Add(new Vector3d(-r, 0, 0));
            var right_cir = new CircularArc3d(bottom, right, top);
            var left_cir  = new CircularArc3d(top, left, bottom);
            var poly      = new Polyline();

            poly.AddVertexAt(0, new Point2d(right_cir.StartPoint.X, right_cir.StartPoint.Y), right_cir.GetArcBulge(), 0, 0);
            poly.AddVertexAt(1, new Point2d(right_cir.EndPoint.X, right_cir.EndPoint.Y), 0, 0, 0);
            poly.AddVertexAt(2, new Point2d(left_cir.StartPoint.X, left_cir.StartPoint.Y), left_cir.GetArcBulge(), 0, 0);
            poly.AddVertexAt(3, new Point2d(left_cir.EndPoint.X, left_cir.EndPoint.Y), 0, 0, 0);
            poly.LayerId = c.LayerId;
            tr.BlockTableRecordCurrentSpace.AppendEntity(poly);
            tr.AddNewlyCreatedDBObject(poly, true);
            c.Erase();
            return(poly);
        }
Пример #9
0
        public static void MagicReplaceCommand()
        {
            // objects initializing
            var nomutt = Convert.ToInt32(Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("nomutt"));
            var doc    = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            var ed     = doc.Editor;
            var db     = doc.Database;

            try {
                using (doc.LockDocument()) {
                    using (var tr = new QuickTransaction()) {
                        var toreplace = tr.GetImpliedOrSelect(new PromptSelectionOptions());
                        if (toreplace == null)
                        {
                            return;
                        }
                        ed.WriteMessage("\nSelect destinion block: ");
                        var totype = Quick.SelectSingle();
                        ed.WriteMessage("\n");
                        if (totype == null)
                        {
                            return;
                        }
                        var masterblock = (BlockTableRecord)tr.GetObject(((BlockReference)tr.GetObject(totype ?? ObjectId.Null, OpenMode.ForWrite)).BlockTableRecord, OpenMode.ForRead);
                        var bt          = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                        var @new        = bt[masterblock.Name];
                        Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("nomutt", 0);
                        var notparsed = 0;
                        var flattern  = false;
                        var objs      = toreplace.Cast <SelectedObject>().Select(o => tr.GetObject(o.ObjectId, OpenMode.ForRead) as Entity).ToArray();
                        if (objs.Any(o => (o as BlockReference)?.Position.Z > 0))
                        {
                            flattern = Quick.AskQuestion("Should flattern blocks with Z value", true) ?? false;
                        }
                        var os = new List <ObjectId>();
                        foreach (var ent in objs)
                        {
                            var oldblk = ent as BlockReference;
                            if (oldblk == null)
                            {
                                //not block..
                                notparsed++;
                                continue;
                            }
                            var p      = oldblk.Position;
                            var ip     = flattern ? new Point3d(p.X, p.Y, 0) : p;
                            var scl    = oldblk.ScaleFactors;
                            var rot    = oldblk.Rotation;
                            var newblk = new BlockReference(ip, @new);
                            newblk.SetPropertiesFrom(ent);
                            newblk.Rotation     = rot;
                            newblk.ScaleFactors = scl;
                            tr.BlockTableRecordCurrentSpace.AppendEntity(newblk);
                            tr.AddNewlyCreatedDBObject(newblk, true);
                            ApplyAttributes(db, tr, newblk);
                            oldblk.UpgradeOpen();
                            oldblk.Erase();
                            oldblk.Dispose();
                            os.Add(newblk.ObjectId);
                        }
                        Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("nomutt", 1);
                        tr.Commit();
                        if (notparsed > 0)
                        {
                            ed.WriteMessage($"{notparsed} are not blocks and were not replaced.\n");
                        }
                        ed.WriteMessage($"{toreplace.Count} were replaced to block {masterblock.Name} successfully.\n");
                        Quick.SetSelected(os.ToArray());
                    }
                }
            } catch (Exception ex) {
                ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
            } finally {
                Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("nomutt", nomutt);
            }
        }