明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1649|回复: 0

[事件] 在修改一条直线的线型类型,修改后,还是一条直线,求高手帮忙看看

[复制链接]
发表于 2014-2-28 12:04 | 显示全部楼层 |阅读模式
下列代码是想实现 先删除一条直线再画一条包含文本的复杂AutoCAD线型 的直线,可是代码执行后还是一条直线没有显示包含文本的复杂AutoCAD线型  ,求高手看看
private void btn_addlinetype_Click(object sender, EventArgs e)
        {
            try
            {
                if (txt_LineContent.Text != "")
                {
                    MessageBox.Show("请在AutoCAD2010 中选择要修改的线段,选择完成后请单击---鼠标右键---返回界面", "选择线段", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.WindowState = FormWindowState.Minimized;//  goto跳回语句
                    Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
                    Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                    Database db = doc.Database;
                    DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
                    using (Transaction trans = doc.TransactionManager.StartTransaction())
                    {
                    Loop: PromptSelectionResult psr = ed.GetSelection();
                        if (psr.Status == PromptStatus.OK)
                        {
                            if (MessageBox.Show("是否确定选择线段?是 表示确定,否 表示重新选择!", "选择提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                            {
                                goto Loop;//goto跳回到选择点
                            }
                            if (psr.Value.Count == 1)
                            {
                                foreach (ObjectId GetSelectId in psr.Value.GetObjectIds())
                                {
                                    Entity ent = trans.GetObject(GetSelectId, OpenMode.ForWrite) as Entity;
                                    string TypeName = ent.GetType().Name.ToString();
                                    if (TypeName == "Line")
                                    {
                                        Line ln = ent as Line;
                                        StartPoint = ln.StartPoint;
                                        EndPoint = ln.EndPoint;
                                        ent.Erase();
                                        ed.UpdateScreen();
                                    }
                                }
                            }
                            else
                            {
                                MessageBox.Show("不能选择多条线段或者不选,只能选择一条,请重新选择", "选择线段", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                ed.UpdateScreen();
                                goto Loop;//goto跳回到选择点
                            }
                        }
                        trans.Commit();
                        ed.UpdateScreen();
                        //ObjectId ltId = m_LoadLineTypeName(ed, "ZIGZAG");
                        CreateComplexLinetype(doc, ed);
                        this.Show();
                    }
                    docLock.Dispose();

                }
                else
                {
                    MessageBox.Show("线型内容不能为空,请输入内容!", "输入线型内容", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txt_LineContent.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        Point3d StartPoint=new Point3d();
        Point3d EndPoint = new Point3d();
        public void CreateComplexLinetype(Document doc, Editor ed)
        {
            try
            {
                Database db = doc.Database;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    // 我们使用文本样式表来访问"Standard"文本样式,用于文本部分
                    TextStyleTable tt = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead);

                    // 获取线型表
                    LinetypeTable lt = (LinetypeTable)tr.GetObject(db.LinetypeTableId, OpenMode.ForWrite, true);
                    // 创建新的线型表记录...
                    LinetypeTableRecord ltr = new LinetypeTableRecord();

                    // ... 设置线型表记录的属性
                    ltr.Name = "COLD_WATER_SUPPLY";
                    ltr.AsciiDescription =
                      "Cold water supply ---- RS ---- RS ---- RS ----";
                    ltr.PatternLength = 0.9;
                    ltr.NumDashes = 3;

                    // 短划线 #1
                    ltr.SetDashLengthAt(0, 0.5);

                    // 短划线 #2
                    ltr.SetDashLengthAt(1, -0.2);
                    ltr.SetShapeStyleAt(1, tt["Standard"]);
                    ltr.SetShapeNumberAt(1, 0);
                    ltr.SetShapeOffsetAt(1, new Vector2d(-0.1, -0.05));
                    ltr.SetShapeScaleAt(1, 0.1);
                    ltr.SetShapeIsUcsOrientedAt(1, false);
                    ltr.SetShapeRotationAt(1, 0);
                    ltr.SetTextAt(1, "RS");

                    // 短划线 #3
                    ltr.SetDashLengthAt(2, -0.2);

                    // 把新线型添加到线型表
                    ObjectId ltId = lt.Add(ltr);
                    tr.AddNewlyCreatedDBObject(ltr, true);

                    // 使用这个线型创建一条测试直线
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

                    Line line = new Line(StartPoint,EndPoint);
                    line.SetDatabaseDefaults(db);
                    line.LinetypeId = ltId;
                    btr.AppendEntity(line);
                    tr.AddNewlyCreatedDBObject(line, true);
                    tr.Commit();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-4-16 20:23 , Processed in 0.279619 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表