- 积分
- 692
- 明经币
- 个
- 注册时间
- 2004-6-4
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2010-12-23 14:52:45
|
显示全部楼层
想过使用.net开发自定义实体没有(三) -.net代码分析
.net实现的项目:
3.RealDotNetLib:实际的.net实体
我实现的一个简单的实体,这个对象目前必须拷贝到autocad2007目录下,否则它提示找不到。
//[DefaultPropertyAttribute("简单DotNet对象")]
public class SimpleEntity : IRealEntity
{
private Entity _proxy;
private ObjectId _myId;
public Point3d _PtA, _PtAB, _PtB, _PtBA;
[Description("PtBA"),
CategoryAttribute("位置"),
TypeConverterAttribute(typeof(Point3dConverter))]
public Point3d PtBA
{
get { return _PtBA; }
set {
_PtBA = value;
}
}
[Description("PtB"),
CategoryAttribute("位置"),
TypeConverterAttribute(typeof(Point3dConverter))]
public Point3d PtB
{
get { return _PtB; }
set {
_PtB = value;
}
}
[Description("PtAB"),
CategoryAttribute("位置"),
TypeConverterAttribute(typeof(Point3dConverter))]
public Point3d PtAB
{
get { return _PtAB; }
set {
_PtAB = value;
}
}
[Description("PtA"),
CategoryAttribute("位置"),
TypeConverterAttribute(typeof(Point3dConverter))]
public Point3d PtA
{
get { return _PtA; }
set {
_PtA = value;
}
}
public String Text;
public SimpleEntity()
{
PtA = new Point3d();
PtAB = new Point3d();
PtB = new Point3d();
PtBA = new Point3d();
Text = "";
}
#region IRealEntity 成员
[Browsable(false)]
public Entity Proxy
{
get
{
return _proxy;
}
set
{
_proxy = value;
}
}
public void DwgInFields(DwgFiler filer)
{
PtA = filer.ReadPoint3d();
PtAB = filer.ReadPoint3d();
PtB = filer.ReadPoint3d();
PtBA = filer.ReadPoint3d();
}
public void DwgOutFields(DwgFiler filer)
{
filer.WritePoint3d(PtA);
filer.WritePoint3d(PtAB);
filer.WritePoint3d(PtB);
filer.WritePoint3d(PtBA);
}
public string List()
{
return this.ToString();
}
public bool WorldDraw(WorldDraw wd)
{
Point3dCollection pts = new Point3dCollection();
pts.Add(PtA);
pts.Add(PtAB);
pts.Add(PtB);
pts.Add(PtBA);
wd.SubEntityTraits.SetSelectionMarker(1);
//Database db = HostApplicationServices.WorkingDatabase;
//Transaction trans = db.TransactionManager.StartTransaction();
//Entity Proxy=(Entity)trans.GetObject(myId, OpenMode.ForRead);
wd.SubEntityTraits.Color = (short)Proxy.ColorIndex;
//trans.Commit();
wd.Geometry.Polygon(pts);
/*
AcGiTextStyle style;
style.setFileName("txt.shx");
style.setBigFontFileName("");
style.setTextSize(25);
style.loadStyleRec();
AcGePoint3d txtPt((m_PtB.x+m_PtA.x)/2.0,(m_PtB.y+m_PtA.y)/2.0, m_PtA.z);
mode->geometry().text(txtPt, AcGeVector3d::kZAxis,
(m_PtAB-m_PtA),m_Text,m_Text.GetLength(),Adesk::kFalse, style);
*/
TextStyle style = new TextStyle();
style.FileName = "txt.shx";
style.BigFontFileName = "";
style.TextSize = 25;
//style.LoadStyleRec;
Point3d txtPt = new Point3d((PtB.X + PtA.X) / 2, (PtB.Y + PtA.Y) / 2, PtA.Z);
wd.Geometry.Text(txtPt, new Vector3d(0, 0, 1), PtAB - PtA, Text, false, style);
return true;
}
public void Dispose()
{
}
public void TransformBy(Matrix3d transform)
{
PtA = PtA.TransformBy(transform);
PtAB = PtAB.TransformBy(transform);
PtB = PtB.TransformBy(transform);
PtBA = PtBA.TransformBy(transform);
}
#endregion
private string name, email;
[CategoryAttribute("用户信息"), DescriptionAttribute("设置消费者姓名")]
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
[CategoryAttribute("用户信息"), DescriptionAttribute("设置消费者Email地址")]
public string Email
{
get
{
return email;
}
set
{
email = value;
}
}
}
4.mgPolyTestVB:使用.net代码在cad中绘制实体
<Autodesk.AutoCAD.Runtime.CommandMethod("ttt")> _
Public Sub TestFunction()
' get the working databasre
Dim db As Database = HostApplicationServices.WorkingDatabase
' get the transaction manager for the working database
Dim tm As DBTransMan = db.TransactionManager
' create a new transaction
Dim myT As Transaction = tm.StartTransaction()
' start our try/catch block
Try
'Assembly.LoadFrom("RealEntityLib.dll")
' create a poly samp object via the .net wrapper
Dim poly As New Tomcad.CustomObject.DotNetEntity() //用manager class生成CustomEntityDBX
' set the properties for the poly
Dim simpEntity As New SimpleEntity()
simpEntity.PtA = New Point3d(0, 0, 0)
simpEntity.PtAB = New Point3d(100, 0, 0)
simpEntity.PtB = New Point3d(100, 200, 0)
simpEntity.PtBA = New Point3d(0, 200, 0)
poly.SetManagedObject(simpEntity) //这步很关键,将CustomEntityDBX的.net句柄赋值,指向我们生成的SimpleEntity。
' get the blocktable as before, but lets open it for read within the transaction manager
Dim bt As BlockTable = CType(tm.GetObject(db.BlockTableId, OpenMode.ForRead, False), BlockTable)
' do the same again but for the model space itself, we will need to open model space for write as
' we will be adding to it
Dim btr As BlockTableRecord = CType(tm.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite, False), BlockTableRecord)
' add the new line to the model space as before
btr.AppendEntity(poly)
' and then make sure that the transaction knows about this new object
tm.AddNewlyCreatedDBObject(poly, True)
' finally commit the changes
myT.Commit()
Catch ex As Exception
' an error occurred
' ...
Finally
' for the transactions we need to call the dispose to finish off
myT.Dispose()
End Try
5.TomPropertyMgr:根据kean代码实现的对象属性面板,可选。因为使用autocad的com对象接口实现自定义实体的动态属性太复杂了,我搞不定,就借用kean的代码,另外显示一个属性面板。
|
|