[求助]关于在CAD中创建两根直线的中线
现在想做一个小的开发,就是创建两条直线的中线。搜索了站内的帖子,没有发现相关内容。然后自己写了一个以后,在vs2010中没有报错,但是到CAD里面运行后,发生致命错误。
小弟新手上路,望指导。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
namespace ClassLibrary1
{
public class Class1
{
public static void setline()
{
Editor acDocEd = Application.DocumentManager.MdiActiveDocument.Editor;
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// 创建一个 TypedValue 数组,用于定义过滤条件
TypedValue[] acTypValAr = new TypedValue;
acTypValAr.SetValue(new TypedValue((int)DxfCode.Start,"Line"), 0);
// 赋值过滤条件给 SelectionFilter 对象
SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);
// 要求在图形区域中选择对象
PromptSelectionResult acSSPrompt;
acSSPrompt = acDocEd.GetSelection(acSelFtr);
// 如果提示状态是 OK,对象就被选择了
if (acSSPrompt.Status == PromptStatus.OK)
{
SelectionSet acSSet = acSSPrompt.Value;
int t = acSSet.Count;
int i;
Point3dCollection pointstart = new Point3dCollection();
Point3dCollection pointend = new Point3dCollection();
// 选择集中的对象
for (i = 0; i <= t-1 ; ++i)
{
if (acSSet != null)
{
Entity acEnt = acTrans.GetObject(acSSet.ObjectId,
OpenMode.ForWrite) as Entity;
if (acEnt != null)
{
Line l = acEnt as Line;
pointstart = l.StartPoint;
pointend = l.EndPoint;
}
}
}
// 以只读方式打开块表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// 以写方式打开模型空间块表记录
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl,
OpenMode.ForWrite) as BlockTableRecord;
Point3d pt1 = pointstart + (pointstart - pointstart) / 2;
Point3d pt2 = pointend + (pointend - pointend) / 2;
Line acLine = new Line(pt1, pt2);
acLine.SetDatabaseDefaults();
// 添加新对象到块表记录和事务中
acBlkTblRec.AppendEntity(acLine);
acTrans.AddNewlyCreatedDBObject(acLine, true);
// 保存新对象到数据库中
acTrans.Commit();
}
}
}
}
}
http://bbs.mjtd.com/xwb/images/bgimg/icon_logo.png 该贴已经同步到 大八脚哈的微博 你好学术[汗][汗][汗]
http://bbs.mjtd.com/xwb/images/bgimg/icon_logo.png 来自 小八爪嗯 的新浪微博 Entity acEnt = acTrans.GetObject(acSSet.ObjectId, OpenMode.ForWrite) as Entity;
-----------------------------------------
看到这里有问题,后面没看 if (acSSet != null)
{
------------------
这里完全是多余,因为前面有int t = acSSet.Count;
若acSSet ==null则在int t = acSSet.Count;
这句就报异常了 pointstart = l.StartPoint;
pointend = l.EndPoint;
-----------------------
这两句编译时没报错? 楼上分析全中 Sage. 发表于 2012-12-5 20:21 static/image/common/back.gif
楼上分析全中
多谢帮我测试。。。我自己咋嘛没有报错呢- - sieben 发表于 2012-12-5 17:32 static/image/common/back.gif
pointstart = l.StartPoint;
pointend = l.EndPoint ...
多谢指出错误,也看到了Sage的帮忙测试。我自己弄的时候咋没报错
我想问一下一种形式的表达,就是像point3d的数组一样,可以使用points【2】这种形式来调用其中的第3个点。不知道我表达清楚没,望赐教。 我不知道你所说的"没报错"是指什么,我应该帮不到你了。 看看我的
public void MiddleLine()
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
PromptSelectionOptions selOpts = new PromptSelectionOptions();
selOpts.MessageForAdding = "选择平行直线段线段";
SelectionFilter sf = new SelectionFilter(new TypedValue[] {new TypedValue(0,"Line")});
PromptSelectionResult selRes = ed.GetSelection(selOpts,sf);
if (selRes.Status != PromptStatus.OK)
{
// throw new System.Exception("错误或用户取消");
return;
}
//需要处理的直线段集合
List<ObjectId> lineObjectIds = new List<ObjectId>(selRes.Value.GetObjectIds());
using (Transaction trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
{
Database db = HostApplicationServices.WorkingDatabase;
BlockTable bt = trans.GetObject(db.BlockTableId,OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
//选取选集第一条线段作为起始线段
ObjectId fLineId = lineObjectIds;
ObjectId sLineId = lineObjectIds;
Line firstLine = trans.GetObject(fLineId, OpenMode.ForRead) as Line;
Line secondLine = trans.GetObject(sLineId, OpenMode.ForRead) as Line;
Line middleLine;
//判断两条线段的方向做出不同处理
if (firstLine.Angle == secondLine.Angle)
{
middleLine = new Line(MiddlePoint(firstLine.StartPoint,secondLine.StartPoint), MiddlePoint(firstLine.EndPoint,secondLine.EndPoint));
ed.WriteMessage(firstLine.Angle+"\r\n"+secondLine.Angle);
}else if ((firstLine.Angle < secondLine.Angle) ? (firstLine.Angle + 180 == secondLine.Angle) : (firstLine.Angle == 180 + secondLine.Angle))
{
middleLine = new Line(MiddlePoint(firstLine.StartPoint,secondLine.EndPoint), MiddlePoint(firstLine.EndPoint,secondLine.StartPoint));
ed.WriteMessage(firstLine.Angle+"\r\n"+secondLine.Angle);
}else
{
MessageBox.Show("所选择线段为非平行线段,请重新选择平行线段!");
return;
}
middleLine.ColorIndex = 210;
btr.AppendEntity(middleLine);
trans.AddNewlyCreatedDBObject(middleLine,true);
trans.Commit();
}
}
public Point3d MiddlePoint(Point3d p1, Point3d p2)
{
return new Point3d((p1.X + p2.X)/2.0,(p1.Y + p2.Y)/2.0,(p1.Z + p2.Z)/2.0);
}
页:
[1]
2