コード例 #1
0
        public static void AddCurveCommand()
        {
            double GetAngle(Point2d a, Point2d b)
            {
                double xDiff = b.X - a.X;
                double yDiff = b.Y - a.Y;

                return(Math.Atan2(yDiff, xDiff) * 180.0d / Math.PI);
            }

            try {
                var options = new PromptEntityOptions("\nSelect a point on a polyline: ");
                options.SetRejectMessage("Invalid Object.");
                options.AddAllowedClass(typeof(Polyline), true);
                options.AddAllowedClass(typeof(Polyline2d), true);
                options.AddAllowedClass(typeof(Polyline3d), true);

                var result = Quick.Editor.GetEntity(options);
                if (result.Status != PromptStatus.OK)
                {
                    return;
                }
                var lineId    = result.ObjectId;
                var pickpoint = result.PickedPoint.ToPoint2D();
                var qtarget   = Quick.Editor.GetPoint(new PromptPointOptions("Pick the strech point.")
                {
                    AllowNone = false, BasePoint = pickpoint.ToPoint3D(), UseBasePoint = true, UseDashedLine = true
                });
                if (qtarget.Status != PromptStatus.OK)
                {
                    return;
                }
                var target = qtarget.Value.ToPoint2D();
                using (var tr = new QuickTransaction()) {
                    try {
                        var poly = (Curve)tr.GetObject(lineId, OpenMode.ForWrite);
                        if (poly is Polyline2d)
                        {
                            poly = poly.ConvertToPolyline(tr);
                        }
                        if (poly is Polyline3d)
                        {
                            throw new InvalidCastException("Can't be applied on polyline of type Polyline3d.");
                        }
                        var distance = target.GetDistanceTo(pickpoint);

                        var pointRight = poly.OffsetToEnd(result.PickedPoint, distance);
                        var pointLeft  = poly.OffsetToStart(result.PickedPoint, distance);
                        if (!pointRight.Item2 || !pointLeft.Item2)
                        {
                            return;
                        }

                        var rightcut = poly.BreakOnPoint(pointRight.Item1, tr, false);
                        poly = (Polyline)rightcut[0];  //right poly
                        var rightpoly = (Polyline)rightcut[1];
                        var leftcut   = poly.BreakOnPoint(pointLeft.Item1, tr, false);
                        var leftpoly  = (Polyline)leftcut[0];
                        leftcut[1].UpgradeOpen();
                        leftcut[1].Erase();

                        var buldge = Math.Tan((90d * 0.85 * Deg2Rad) / 4);
                        leftpoly.AddVertexAt(leftpoly.NumberOfVertices, target, 0, leftpoly.GetStartWidthAt(leftpoly.NumberOfVertices - 1), leftpoly.GetEndWidthAt(leftpoly.NumberOfVertices - 1));
                        leftpoly.SetBulgeAt(leftpoly.NumberOfVertices - 2, buldge);
                        rightpoly.AddVertexAt(0, target, buldge, leftpoly.GetStartWidthAt(0), leftpoly.GetEndWidthAt(0));

                        void setdir(bool right)
                        {
                            if (right)
                            {
                                leftpoly.SetBulgeAt(leftpoly.NumberOfVertices - 2, buldge);
                                rightpoly.SetBulgeAt(0, buldge);
                            }
                            else
                            {
                                leftpoly.SetBulgeAt(leftpoly.NumberOfVertices - 2, -buldge);
                                rightpoly.SetBulgeAt(0, -buldge);
                            }
                        }

                        setdir(Settings.addcurve_direction as bool? ?? true);

                        tr.Transaction.TransactionManager.QueueForGraphicsFlush();

                        // ReSharper disable once AssignmentInConditionalExpression
                        var ask = (bool?)Quick.AskQuestion("Reverse Direction?", false) ?? false;
                        if (ask)
                        {
                            Settings.addcurve_direction = !(Settings.addcurve_direction as bool? ?? false);
                            setdir(Settings.addcurve_direction);
                        }
                        tr.Transaction.TransactionManager.QueueForGraphicsFlush();


                        // ReSharper disable once AssignmentInConditionalExpression
                        if (Settings["addcurve_join"] = Quick.AskQuestion("Join Polylines?", (Settings["addcurve_join"]) ?? !true))
                        {
                            new[] { leftpoly.Join(rightpoly, tr) }.SetSelected();
                        }
                        else
                        {
                            new[] { leftpoly, rightpoly }.SetSelected();
                        }
                        tr.Commit();
                    } catch (Exception ex) {
                        Quick.WriteLine(ex.Message + "\n" + ex.StackTrace);
                    }
                }
            } catch (Exception ex) {
                Quick.WriteLine(ex.Message + "\n" + ex.StackTrace);
            }
        }
コード例 #2
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);
            }
        }