前面开了一个头,这里添加几个功能的实现,

   //添加图层
private void LoadLayer()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
using (Transaction tr = acDoc.TransactionManager.StartTransaction())
{
//指定点图层
LayerTable lt = (LayerTable)tr.GetObject(acDoc.Database.LayerTableId, OpenMode.ForRead);
if (!lt.Has("test"))//判断是否存在
{//不存在添加
var LayerID = ObjectId.Null;
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "test";
ltr.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(
Autodesk.AutoCAD.Colors.ColorMethod.ByColor, );
lt.UpgradeOpen();
LayerID = lt.Add(ltr);
tr.AddNewlyCreatedDBObject(ltr, true);
}
tr.Commit();
}
} private bool isBCAD = false;
/// <summary>
/// 列出所有对象
/// </summary>
[CommandMethod("LE",CommandFlags.Transparent)]
public void ListEntities()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Editor acadEd = Application.DocumentManager.MdiActiveDocument.Editor;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
int nCnt = ;
acDoc.Editor.WriteMessage("\nModel space objects: ");
foreach (ObjectId acObjId in acBlkTblRec)
{ acDoc.Editor.WriteMessage("\n" + acObjId.ObjectClass.DxfName + ":" + acObjId.Handle.Value.ToString());
nCnt = nCnt + ;
}
if (nCnt == )
{
acDoc.Editor.WriteMessage("\nNo objects found.");
}
else
{
acDoc.Editor.WriteMessage("\nTotal {0} objects.", nCnt);
}
}
} private int _id = ;
/// <summary>
/// 生成闭合区域,
/// </summary>
[CommandMethod("SetArea", CommandFlags.Transparent)]
public void SetArea()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Editor acadEd = Application.DocumentManager.MdiActiveDocument.Editor; PromptPointOptions ptOpt = new PromptPointOptions("选择点:\n");
PromptPointResult ptRel = null;
ObjectId[] ids = null; ptRel = acadEd.GetPoint(ptOpt);
Database acCurDb = acDoc.Database;
//该方法必须是ObjectArx2011以后的版本才支持,以前的版本可以用-bo命令加SelectImplied方法得到的选择集来实现该功能
var bound = acadEd.TraceBoundary(ptRel.Value, true); using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
foreach (var dbObj in bound)
{
var line = dbObj as Polyline;
if (acCurDb != null) line.Color = Color.FromColor(System.Drawing.Color.Red);
BlockTable bt = (BlockTable)acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)acTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
var id = btr.AppendEntity(line);//添加实体
acTrans.AddNewlyCreatedDBObject(line, true);//保存到cad数据库
//下面为实体添加扩展属性,这里扩展属性添加到实体的扩展字典中,也可以直接添加到实体的XData的属性中
line.CreateExtensionDictionary();//为实体创建扩展字典
var dic = acTrans.GetObject(line.ExtensionDictionary, OpenMode.ForWrite) as DBDictionary;//获取实体的扩展字典,DBDictionary,可以用通过SetAt和GetAt设置和获取
var xrecord=new Xrecord();//创建Xrecord对象。里边保存具体的扩展属性对象
xrecord.Data = new ResultBuffer(new TypedValue((int) DxfCode.Text, "测试1"));//添加属性对象都保存为ResultBuffer对象对应不同的DxfCode码
dic.SetAt("exta", xrecord);//添加到字典中,继承自DBObject的对象都可以作为value添加到字典中
acTrans.AddNewlyCreatedDBObject(xrecord, true);//因为添加的扩展属性也是DBObject对象,要添加到cad的数据库中
//这里添加一个cad的DataTable对象到字典
var dt = new DataTable();
dt.TableName = "polyattri";
dt.AppendColumn(CellType.Integer, "line_id");
var lineid = new DataCell();
lineid.SetInteger(_id);
var row = new DataCellCollection();
row.Add(lineid);
dt.AppendRow(row, true);
dic.UpgradeOpen();
dic.SetAt("attr", dt);
acTrans.AddNewlyCreatedDBObject(dt, true);
_id++;
acadEd.WriteMessage(line.ObjectId.ToString());
}
acTrans.Commit();
} }
//导入线型,根据线型文件和线名来加载线型,后面可以通过设置当前的环境变量来改变当前线型
//路径如果为文件名,cad会自动在主程序的相对路径下搜索,如果是自定线型需要指定绝对路径,
//如果线型文件关联SHX文件,记得要一起不然会加载失败
private void loadlinetype()
{
var lineTypeFile = "acadiso.lin";
string lineTypeName = "acad_iso03w100";
ObjectId idRet = ObjectId.Null;
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Editor acadEd = Application.DocumentManager.MdiActiveDocument.Editor;
Database db = acDoc.Database;
using (Transaction trans = db.TransactionManager.StartTransaction())
{ LinetypeTable ltt = (LinetypeTable)trans.GetObject(db.LinetypeTableId, OpenMode.ForWrite);
LinetypeTableRecord lttr = new LinetypeTableRecord(); if (ltt.Has(lineTypeName))
{
idRet = ltt[lineTypeName];
}
else
{
try
{
db.LoadLineTypeFile(lineTypeName, lineTypeFile);//加载线型
idRet = ltt[lineTypeName];
}
catch (System.Exception ex)
{
acadEd.WriteMessage(ex.Message);
}
finally
{
trans.Commit();
}
}
}
}
//设置线型
[CommandMethod("SLT")]
public void SetLineType()
{
string test = Application.GetSystemVariable("CELTYPE").ToString();
Application.SetSystemVariable("CELTYPE", "acad_iso03w100");//通过改变环境变量来设置线型,CELTYPE对应当前线型
}
//删除选中//这里实现先选中后删除的操作
[CommandMethod("DelS")]
public void DelSelect()
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Editor acadEd = Application.DocumentManager.MdiActiveDocument.Editor; SelectionSet acSSet;
var selectionRes = acadEd.SelectImplied();//对应选中的选择集
Database acCurDb = acDoc.Database;
if (selectionRes.Status == PromptStatus.OK)
{
acSSet = selectionRes.Value; using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
foreach (var v in acSSet.GetObjectIds())
{
DBObject dbObj = acTrans.GetObject(v, OpenMode.ForWrite);
dbObj.Erase(true);
}
acTrans.Commit();
} }
else
{
acDoc.Editor.WriteMessage(selectionRes.Status.ToString());
}
} //选中线病获取上面设置的扩展属性,上面怎么添加的这边就怎么解析Xrecord对象
[CommandMethod("SLL")]
public void SelectLine()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Polyline line;
if(!SelectEntity(out line,"多段线"))return;
if (line != null)
{
using (var tr = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
var dic = tr.GetObject(line.ExtensionDictionary, OpenMode.ForRead) as DBDictionary;
ObjectId id = dic.GetAt("attr");
var dt = tr.GetObject(id, OpenMode.ForRead) as DataTable;
int index = dt.GetColumnIndexAtName("line_id");
var att = dt.GetCellAt(, index).Value;
ed.WriteMessage(att + "\n"); id = dic.GetAt("exta");
var xrec = tr.GetObject(id, OpenMode.ForRead) as Xrecord;
foreach (var xdata in xrec.Data)
{
ed.WriteMessage(xdata.Value.ToString() + "\n");
} } // ed.WriteMessage("\n{0},{1}", line.StartPoint.X, line.StartPoint.Y);
//ed.WriteMessage("\n{0},{1}", line.EndPoint.X, line.EndPoint.Y);
} }
//添加文字
[CommandMethod("SLP")]
public void SelectPoint()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
var sel = ed.GetPoint("选择点");
var db = Application.DocumentManager.MdiActiveDocument.Database;
using (var tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
DBText txt = new DBText();
txt.Position = sel.Value;
txt.Height = 0.5;
txt.TextString = "测试"; btr.AppendEntity(txt);
tr.AddNewlyCreatedDBObject(txt, true);
tr.Commit();
}
} //选中所有的多段线
[CommandMethod("SLPS")]
public void SelectPolyline()
{
var ed = Application.DocumentManager.MdiActiveDocument.Editor;
//添加过滤条件,可以添加类型/图层条件不同的dxfcode对应不同的对象
var tvs = new TypedValue[] { new TypedValue((int)DxfCode.Start, "LWPOLYLINE") };//LWPOLYLINE 对应Polyline对象,这里的类型字段可以用前面的列举对象来查看···,
var sf = new SelectionFilter(tvs);
var sel = ed.SelectAll(sf);
if (sel.Value == null) return;
using (var tr=Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
var ids = sel.Value.GetObjectIds();
foreach (var obj in ids)
{
var line = tr.GetObject(obj, OpenMode.ForRead) as Polyline;
ed.WriteMessage(line.ObjectId.ToString());
}
tr.Commit();
} } //跳转到某点,用当前视图的大小来设置view的大小,也可以直接指定大小
[CommandMethod("FLYTOP")]
public void FlytoPoint()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
var db = Application.DocumentManager.MdiActiveDocument.Database;
var sel = ed.SelectPrevious();//ed.SelectLast();
using (Transaction acTrans = db.TransactionManager.StartTransaction())
{
if(sel.Value==null)return;
if(sel.Value.GetObjectIds().Length<)return;
var ent=acTrans.GetObject(sel.Value.GetObjectIds()[], OpenMode.ForRead) as Entity;
if (ent != null)
{
ViewTableRecord view = new ViewTableRecord();
Extents3d ext = ent.GeometricExtents;
ext.TransformBy(ed.CurrentUserCoordinateSystem.Inverse()); Vector3d vec1 = new Vector3d(new double[] { , , });
ext.MaxPoint.Add(vec1);
ext.MinPoint.Subtract(vec1);
view.CenterPoint = new Point2d((ext.MaxPoint.X + ext.MinPoint.X) / ,
(ext.MaxPoint.Y + ext.MinPoint.Y) / );
view.Height = ed.GetCurrentView().Height;
view.Width = ed.GetCurrentView().Height;
ed.SetCurrentView(view);
Application.DocumentManager.MdiActiveDocument.TransactionManager.FlushGraphics();
}
acTrans.Commit();
} }
//选择一个实体对象,取消或者选中返回
private bool SelectEntity<T>(out T obj, string entityName) where T : Entity
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
var db = Application.DocumentManager.MdiActiveDocument.Database;
PromptEntityOptions peo = new PromptEntityOptions(string.Format("\n请选择{0}:", entityName));
peo.SetRejectMessage(string.Format("\n必须是{0}!", entityName));
peo.AddAllowedClass(typeof(T), false);
obj = null;
while (true)
{
var selection = ed.GetEntity(peo);
if (selection.Status == PromptStatus.OK)
{
using (var tr = db.TransactionManager.StartTransaction())
{
obj = tr.GetObject(selection.ObjectId, OpenMode.ForRead) as T;
tr.Commit();
return true;
}
}
if (selection.Status == PromptStatus.Cancel)
{
return false;
}
}
} //隐藏图层
[CommandMethod("CTL")]
public void LayerEnable()
{
var doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction trs = db.TransactionManager.StartTransaction())
{
LayerTable lt = trs.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
var layer = trs.GetObject(lt["test"], OpenMode.ForWrite) as LayerTableRecord;
layer.IsOff = layer.IsOff ? false : true; trs.Commit();
}
}
    
 //获取上一次选中或者创建的对象
public Entity[] GetLast()
{
List<Entity> list = new List<Entity>();
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
var db = Application.DocumentManager.MdiActiveDocument.Database;
var sel = ed.SelectPrevious();//上一次选中的
if (sel.Value == null) sel = ed.SelectLast();//如果为空就设置为上一次创建的对象
using (Transaction acTrans = db.TransactionManager.StartTransaction())
{
if (sel.Value == null) return null;
ObjectId[] ids = sel.Value.GetObjectIds();
foreach (var id in ids)
{
list.Add(acTrans.GetObject(id, OpenMode.ForRead) as Entity);
}
acTrans.Commit();
}
return list.ToArray();
}

  
 

另:

1 cad2012 调试的时候form中的断点进不去,需要把NEXTFIBERWORLD变量改为0;这么改以后用com启动cad加载dll时有时候会报错, 调试和使用的时候注意设置这个

2

C#+ObjectArx CAD二次开发(2)的更多相关文章

  1. C# ObjectArx cad二次开发

    1 环境搭建:安装CAD 和objectArx库,这里安装的是cad2012和objectArx2010 ,vs是2010 2 新建一个类库项目,引用objectArx安装目录下inc文件夹下的AcD ...

  2. CAD二次开发起步

    1 环境搭建(VS 2017 + CAD 2019) CAD版本一般要比VS版本晚两年以上,比如我现在使用VS2017,则CAD要用2019版,至于CAD2018能不能用没试验过,有兴趣的小伙伴可以试 ...

  3. Cad 二次开发关于SelectCrossingPolygon和SelectFence返回结果Status为error的小测试

    CAD2008的二次开发,有个很奇怪的现象,只要你选择的点集不在当前视图上SelectCrossingPolygon和SelectFence返回结果Status就会为error,所以要获取正确的结果, ...

  4. CAD二次开发

    用C#有一段时间了,由于单位需要,开始接触CAD二次开发,网上一搜,加入CAD开发的群,零零碎碎看了一些文章和博客,没有系统地的知识,能解决一些小问题.最近开始系统学习,再次推荐两本书,一本事纸质版的 ...

  5. 推荐两个不错的CAD二次开发(.Net)手册

    推荐两个不错的CAD二次开发(.Net)手册 http://www.mjtd.com/helpcenter/netguide/index.html http://www.ceesky.com/book ...

  6. CAD 二次开发 -- 自动加载开发的DLL

    CAD二次开发可以采用写扩展DLL的方式实现.该DLL的函数可以被CAD调用. 但是调用前,必须用命令netload 将该dll加载到CAD. 其实可以修改注册表,当CAD软件启动后,自动加载扩展DL ...

  7. C#进行CAD二次开发环境配置

    最近被公司分配到了做CAD二次开发.也是初次接触这方面的东西,其实是有些无从下手的感觉.因为公司这边也没有人有时间带我,只能是自己看书,然后再写一些Demo,再结合实际的应用来一点点的学习.废话不多说 ...

  8. CAD二次开发中浮动面板不浮动的问题

    CAD二次开发中创建了一个浮动面板,想让它创建出来后以浮动状态显示, 但是DockSides.None设置完后,面板还是不浮动.搞了很久,最后原来是 需要先设置Visible,再设置DockSides ...

  9. CAD二次开发控件,dwg控件,网页DWG控件,手机浏览编辑DWG控件

    梦想绘图插件5.2(MxDraw5.2) 是国内最强,最专业的CAD开发插件(控件),不需要AutoCAD就能独立运行. 控件使用VC 2010开发,具有30万行代码规模,最早从2007年第一个版本完 ...

随机推荐

  1. application的使用(实现计数器)

    application在整个WEB项目中共享使用数据. 常用方法: getAttribute(); setAttribute();示列: <%    Object count=applicati ...

  2. 根据枚举获取枚举的Description特性值

    首先定义一个枚举:两个值:已确认.未确认. public enum ConfirmStatusEnum { [Description("未确认")] unconfirmed = , ...

  3. SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树

    数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

  4. Qt 学习之路 2(47):视图选择

    Qt 学习之路 2(47):视图选择 豆子 2013年3月28日 Qt 学习之路 2 34条评论 选择是视图中常用的一个操作.在列表.树或者表格中,通过鼠标点击可以选中某一项,被选中项会变成高亮或者反 ...

  5. scrapy的 安装 及 流程 转

    安装 linux 和 mac 直接  pip install scrapy 就行 windows 安装步骤         a. pip3 install wheel       b. 下载twist ...

  6. Kibana6.x.x源码结构分析笔记

  7. python (1) 还不是大全的小问题

    1.pythone 获取系统时间 import datetime nowTime=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')#现在 pa ...

  8. 在Linux系统中,使用useradd命令新建用户后,登录该用户时shell开头为$,不显示用户名和路径,如下:

    在~/.bash_profile中加入以下代码,若无该文件可自行创建: vi ~/.bash_profile #加入 #export PS1='[u@h W]$' 大写W代表最后路径,小写w代表详细路 ...

  9. java中的线程(2):如何正确停止线程之3种常见停止方式

    1.常见停止方式 自定义线程,其中含退出标志位,在run中判断它. 使用interrupt()方法中断线程 使用stop方法暴力终止(已经弃用) 2.使用标志位 class TestThread ex ...

  10. vue中使用对非表单元素使用contenteditable的问题

    先说下问题,再上解决方案: span编辑时有多余空格和回车会影响样式(我用的是span便以此为例) 代码:(有换行符) 效果图: 代码:(无换行符) 效果图: 当在span标签的contentedit ...