乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 41|回复: 2

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

[复制链接]

4

主题

12

帖子

1

银币

初来乍到

Rank: 1

铜币
28
发表于 2012-9-29 17:27:00 | 显示全部楼层 |阅读模式
  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..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 ids = new List(selRes.Value.GetObjectIds());
  42.    
  43.    using (Transaction trans = db.TransactionManager.StartTransaction())
  44.    {
  45.     List lstLines = new List();
  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 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)  pars = new List();
  74.       pars.Add(line.GetParameterAtPoint(OriginPoint));
  75.       BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false);
  76.       DBObjectCollection objs;
  77.       //按原点打断
  78.       objs = line.GetSplitCurves(new DoubleCollection(pars.ToArray()));
  79.       foreach (Line newline in objs)
  80.       {
  81.        btr.AppendEntity(newline);
  82.        trans.AddNewlyCreatedDBObject(newline, true);
  83.       }
  84.       line.UpgradeOpen();
  85.       line.Erase();
  86.       trans.Commit();
  87.      }
  88.     }
  89.    }
  90.   }
  91. }
  92. }

bilai1twv0s.png

bilai1twv0s.png

回复

使用道具 举报

32

主题

651

帖子

8

银币

中流砥柱

Rank: 25

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

使用道具 举报

4

主题

12

帖子

1

银币

初来乍到

Rank: 1

铜币
28
发表于 2012-9-29 22:58:00 | 显示全部楼层

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

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-6-29 14:46 , Processed in 0.269107 second(s), 62 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表