明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2003|回复: 2

【已解决】指定点打断线段

[复制链接]
发表于 2012-9-29 17:27:07 | 显示全部楼层 |阅读模式
本帖最后由 白糖 于 2012-9-30 01:06 编辑

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. using Autodesk.AutoCAD.ApplicationServices;
  8. using Autodesk.AutoCAD.DatabaseServices;
  9. using Autodesk.AutoCAD.EditorInput;
  10. using Autodesk.AutoCAD.Geometry;
  11. using Autodesk.AutoCAD.Runtime;
  12. namespace CADtest
  13. {
  14. ///
  15. /// Description of UserControl1.
  16. ///
  17. public partial class UserControl1 : UserControl
  18. {
  19.   public UserControl1()
  20.   {
  21.    //
  22.    // The InitializeComponent() call is required for Windows Forms designer support.
  23.    //
  24.    InitializeComponent();
  25.    
  26.    //
  27.    // TODO: Add constructor code after the InitializeComponent() call.
  28.    //
  29.   }
  30.   [CommandMethod("test")]
  31.   public void Test()
  32.   {
  33.    Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  34.    Database db = doc.Database;
  35.    Editor ed = doc.Editor;
  36.    PromptSelectionOptions selOpts = new PromptSelectionOptions();
  37.    selOpts.MessageForAdding = "\n请选取线段";
  38.    SelectionFilter sf = new SelectionFilter(new TypedValue[]{new TypedValue(0,"Line")});
  39.    PromptSelectionResult selRes = ed.GetSelection(selOpts,sf);
  40.    if (selRes.Status != PromptStatus.OK) return;
  41.    List<ObjectId> ids = new List<ObjectId>(selRes.Value.GetObjectIds());
  42.    
  43.    using (Transaction trans = db.TransactionManager.StartTransaction())
  44.    {
  45.     List<Line> lstLines = new List<Line>();
  46.     foreach (ObjectId id in ids)
  47.     {
  48.      Entity ent = trans.GetObject(id,OpenMode.ForRead) as Entity;
  49.      if (ent.GetType() == typeof(Line))
  50.      {
  51.       Line line = ent as Line;
  52.       lstLines.Add(line);
  53.      }
  54.     }
  55.     BreakLine(lstLines);
  56.     trans.Commit();
  57.    }
  58.   }
  59.   //从原点打断中线
  60.   public void BreakLine(List<Line> lines)
  61.   {
  62.    Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  63.    Database db = doc.Database;
  64.    Editor ed = doc.Editor;
  65.    
  66.    using (Transaction trans = db.TransactionManager.StartTransaction())
  67.    {
  68.     //原点
  69.     Point3d OriginPoint = new Point3d(0,0,0);
  70.     foreach(Line line in lines)
  71.     {
  72.      MessageBox.Show(line.ObjectId.ToString()+":"+line.GetClosestPointTo(OriginPoint, false).DistanceTo(OriginPoint)+"\n");
  73.      if (line.GetClosestPointTo(OriginPoint,false).DistanceTo(OriginPoint) < Tolerance.Global.EqualPoint)//取得原点所在线段(设置容差)
  74.      {
  75.       List<double> pars = new List<double>();
  76.       pars.Add(line.GetParameterAtPoint(OriginPoint));
  77.       BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false);
  78.       DBObjectCollection objs;
  79.       //按原点打断
  80.       objs = line.GetSplitCurves(new DoubleCollection(pars.ToArray()));
  81.       foreach (Line newline in objs)
  82.       {
  83.        btr.AppendEntity(newline);
  84.        trans.AddNewlyCreatedDBObject(newline, true);
  85.       }
  86.       line.UpgradeOpen();
  87.       line.Erase();
  88.       trans.Commit();
  89.      }
  90.     }
  91.    }
  92.   }
  93. }
  94. }


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
发表于 2012-9-29 22:51:09 | 显示全部楼层
1,你倒是说一下可能哪里出错,有什么提示?
2, if (line.GetClosestPointTo(OriginPoint, false).DistanceTo(OriginPoint) == 0) 这一句或许与你的预期不一样,0.000000001 与0.0是不等的
3, oldLine.UpgradeOpen();
        oldLine.Erase();
        trans.Commit();
oldLine 并非是下方的事务中打开的,你在这里修改应该是有问题的

点评

非常感谢!设置了容差  发表于 2012-9-30 01:07
 楼主| 发表于 2012-9-29 22:58:35 | 显示全部楼层
本帖最后由 白糖 于 2012-9-29 23:00 编辑
sieben 发表于 2012-9-29 22:51
1,你倒是说一下可能哪里出错,有什么提示?
2, if (line.GetClosestPointTo(OriginPoint, false).Distan ...


修改了下代码,确实是求最近距离那里出了问题,求出来的距离非预期,请问怎么解决呢?

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

本版积分规则

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

GMT+8, 2024-11-25 17:33 , Processed in 0.173922 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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