c# - 尝试在 C# 上使用 AutoCAD 类镜像绘图

标签 c# .net visual-studio autocad autodesk

目前我有一个使用 AutoCAD 类绘制的对象,但我可以选择在绘制后对其进行镜像。我不确定该怎么做。

[CommandMethod("DrawTestDoor")]
        public void DrawTestDoor()
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument; 
            var ed = acDoc.Editor;
            XDDoor s1 = new XDDoor();
            PromptKeywordOptions pKeyOpts = new PromptKeywordOptions("");
            pKeyOpts.Message = "\nIt is Rated ";
            pKeyOpts.Keywords.Add("True");
            pKeyOpts.Keywords.Add("False");
            pKeyOpts.Keywords.Default = "True";
            pKeyOpts.AllowNone = true;
            PromptResult pKeyRes = acDoc.Editor.GetKeywords(pKeyOpts);
            bool fireRated = Convert.ToBoolean(pKeyRes.StringResult);

            var promptResultheight = ed.GetString("\nEnter the Frame height: ");
            double height = Convert.ToDouble(promptResultheight.StringResult);

            var promptResultwidth = ed.GetString("\nFrame Width: ");
            double width = Convert.ToDouble(promptResultwidth.StringResult);

            var promptResultFrameDepthChange = ed.GetString("\nEnter the frame depth change ");
            double frameDepthChange = Convert.ToDouble(promptResultFrameDepthChange.StringResult);

            PromptKeywordOptions pKeyOpts1 = new PromptKeywordOptions("");
            pKeyOpts1.Message = "\nDoor Handle Side";
            pKeyOpts1.Keywords.Add("Left");
            pKeyOpts1.Keywords.Add("Right");
            pKeyOpts1.Keywords.Default = "Left";
            pKeyOpts1.AllowNone = true;
            s1.DrawSingleXDDoor(height, width, fireRated, frameDepthChange);
            Matrix2d md = new Matrix2d();
            md.Translation.Mirror()
        }

md.Translation.Mirror() 是我认为需要更改的行。我已经尝试了多种方法来制作镜像,但我一直回到这个问题,因为我不知道 s1 对象被原样保存的是什么。也许认为它需要转换为实体?

public void DrawSingleXDDoor(double height, double width, bool fireRated, double frameDepthChange)
        {
            DrawLid("lid", leafHeight, lidWidth);
        }

public void DrawLid(string type, double height, double width)
        {
            DrawShapes d1 = new DrawShapes();
            DrawComponents xd = new DrawComponents();
            d1.DrawRectangle(0, 0, height, width);
        }
public void DrawRectangle(double startx, double starty, double recHeight, double recWidth)
        {
            double height = recHeight;
            double width = recWidth;
            //Get the drawing document and the dataabses object
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                try
                {
                    BlockTable bt;
                    bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                    BlockTableRecord btr;
                    btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; 
                    Polyline p1 = new Polyline();
                    p1.AddVertexAt(0, new Point2d(startx + 0, starty + 0), 0, 0, 0);
                    p1.AddVertexAt(0, new Point2d(startx + 0, starty + height), 0, 0, 0);
                    p1.AddVertexAt(0, new Point2d(startx + width, starty + height), 0, 0, 0);
                    p1.AddVertexAt(0, new Point2d(startx + width, starty + 0), 0, 0, 0);
                    p1.AddVertexAt(0, new Point2d(startx + 0, starty + 0), 0, 0, 0);
                    p1.SetDatabaseDefaults();
                    btr.AppendEntity(p1);
                    trans.AddNewlyCreatedDBObject(p1, true);
                    trans.Commit();
                }
                catch (System.Exception ex)
                {
                    doc.Editor.WriteMessage("Error encountered: " + ex.Message);
                    trans.Abort();
                }
            }

        }

最佳答案

此示例来自 AutoCAD 2017 .Net SDK。

看起来您错过了在转换上设置镜像点并将镜像副本插入 BlockTable。添加新的 p1 (polyline) 后,下面的代码应该会直接执行。我从 here 得到了这段代码示例.试一试,让我知道。干杯!

 // Define the mirror line
        Point3d acPtFrom = new Point3d(0, 4.25, 0);
        Point3d acPtTo = new Point3d(4, 4.25, 0);
        Line3d acLine3d = new Line3d(acPtFrom, acPtTo);

        // Mirror the polyline across the X axis
        acPolyMirCopy.TransformBy(Matrix3d.Mirroring(acLine3d));

        // Add the new object to the block table record and the transaction
        acBlkTblRec.AppendEntity(acPolyMirCopy);
        acTrans.AddNewlyCreatedDBObject(acPolyMirCopy, true);

关于c# - 尝试在 C# 上使用 AutoCAD 类镜像绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59067046/

相关文章:

c# - asp.net mvc 布局逻辑的最佳位置在哪里

没有 csproj 的 C# 9 顶级程序?

c# - 绑定(bind)自定义类中的键

c# - 如何将可选参数传递给 winform 命令行

.net - 如何管理同一个dll的多个目标平台?

.net - 如何在整个 WPF 应用程序中重用样式?

c# - 使用 Microsoft Cognitive 进行实时说话人识别

c# - 仅在 Visual Studio Code 中出现编译错误

c - 如何使用 VS11 在 C 程序中嵌入和使用资源文件?

c# - Postgres analog for c# SQL Server´s stored procedure CLR