创建一个Employee 对象

2009年3月7日星期六

创建一个Employee 对象

本demo目标,准备创建一个圆,椭圆,多文本对象。这个代码比较难,我看了半天才算搞点清楚,还有点问题。用到了

view plaincopy to clipboardprint?
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;

包括事务处理(Transaction)、对象Id(ObjectId)、符号表(symbol tables,如块表BlockTable和层表LayerTable)以及对象引用。使用的其它一些对象如颜色Color、三维点Point3d和三维向量Vector3d,都和各自的步骤有关,但重点应该放在数据库基础上。

整体代码如下:

view plaincopy to clipboardprint?
using System ;
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;
[assembly:CommandClass(typeof(CsMgdAcad6.Class1))]

namespace CsMgdAcad6
{
public class Class1
{
public Class1()
{
//析构函数
}
public ObjectId CreateEmployeeDefinition()
{
ObjectId newBtrId = new ObjectId();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForWrite);
if (bt.Has("EmployeeBlock"))
{
newBtrId =bt[ "EmployeeBlock"];
}
else
{
Point3d center = new Point3d(10,20,30);
Circle circle = new Circle(center, Vector3d.XAxis, 2);
MText text = new MText();
text.Contents = "拉拉";
text.Location = center;
Ellipse ellipse = new Ellipse(center, Vector3d.ZAxis, new Vector3d(3, 0, 0), 0.5, 0, 0);
ObjectId empId = CreateLayer();
text.LayerId = empId;
circle.LayerId = empId;
ellipse.LayerId = empId;
text.ColorIndex = 2;
circle.ColorIndex = 3;
BlockTableRecord newbtr = new BlockTableRecord();
newbtr.Name = "EmployeeBlock";
newBtrId = bt.Add(newbtr);
trans.AddNewlyCreatedDBObject(newbtr, true);
newbtr.AppendEntity(circle);
newbtr.AppendEntity(text);
newbtr.AppendEntity(ellipse);
trans.AddNewlyCreatedDBObject(circle,true);
trans.AddNewlyCreatedDBObject(text,true);
trans.AddNewlyCreatedDBObject(ellipse,true);
}
trans.Commit();
}
return newBtrId;

}
private ObjectId CreateLayer()
{
ObjectId layerId;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
if (lt.Has("EmployeeLayer"))
{
layerId = lt["EmployeeLayer"];
}
else
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "EmployeeLayer";
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
lt.UpgradeOpen();
layerId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
trans.Commit();
}
}
return layerId;
}

[CommandMethod("create")]
public void createEmployee()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForWrite));
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
BlockReference br = new BlockReference(new Point3d(10, 20, 30), CreateEmployeeDefinition());
}
catch (System.Exception e)
{
ed.WriteMessage("有错误:" + e.Message);
}
finally
{
trans.Dispose();
}

}
}
}
using System ;
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;
[assembly:CommandClass(typeof(CsMgdAcad6.Class1))]

namespace CsMgdAcad6
{
public class Class1
{
public Class1()
{
//析构函数
}
public ObjectId CreateEmployeeDefinition()
{
ObjectId newBtrId = new ObjectId();
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForWrite);
if (bt.Has("EmployeeBlock"))
{
newBtrId =bt[ "EmployeeBlock"];
}
else
{
Point3d center = new Point3d(10,20,30);
Circle circle = new Circle(center, Vector3d.XAxis, 2);
MText text = new MText();
text.Contents = "拉拉";
text.Location = center;
Ellipse ellipse = new Ellipse(center, Vector3d.ZAxis, new Vector3d(3, 0, 0), 0.5, 0, 0);
ObjectId empId = CreateLayer();
text.LayerId = empId;
circle.LayerId = empId;
ellipse.LayerId = empId;
text.ColorIndex = 2;
circle.ColorIndex = 3;
BlockTableRecord newbtr = new BlockTableRecord();
newbtr.Name = "EmployeeBlock";
newBtrId = bt.Add(newbtr);
trans.AddNewlyCreatedDBObject(newbtr, true);
newbtr.AppendEntity(circle);
newbtr.AppendEntity(text);
newbtr.AppendEntity(ellipse);
trans.AddNewlyCreatedDBObject(circle,true);
trans.AddNewlyCreatedDBObject(text,true);
trans.AddNewlyCreatedDBObject(ellipse,true);
}
trans.Commit();
}
return newBtrId;

}
private ObjectId CreateLayer()
{
ObjectId layerId;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);
if (lt.Has("EmployeeLayer"))
{
layerId = lt["EmployeeLayer"];
}
else
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "EmployeeLayer";
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
lt.UpgradeOpen();
layerId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
trans.Commit();
}
}
return layerId;
}

[CommandMethod("create")]
public void createEmployee()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForWrite));
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
BlockReference br = new BlockReference(new Point3d(10, 20, 30), CreateEmployeeDefinition());
}
catch (System.Exception e)
{
ed.WriteMessage("有错误:" + e.Message);
}
finally
{
trans.Dispose();
}

}
}
}

0 评论:

发表评论