这些函数对大家很有用,如果想获取详细源代码请加云幽学院yunyou.ke.qq.com报名免费课程,如果想学习更系统、更全面的知识请报名收费课程,有大量开发案例共享。

1、获取模型空间中所有实体

#region "获取模型空间中所有实体"

/// <summary>

/// 获取模型空间中所有实体

/// </summary>

/// <param name=""></param>

/// <returns>实体ID的列表</returns>

public List<Autodesk.AutoCAD.DatabaseServices.ObjectId> GetEntitiesInModelSpace()

{

List<Autodesk.AutoCAD.DatabaseServices.ObjectId> objects =

new List<Autodesk.AutoCAD.DatabaseServices.ObjectId>();

using (Autodesk.AutoCAD.DatabaseServices.Transaction transaction =

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.

MdiActiveDocument.TransactionManager.StartTransaction())

{

Autodesk.AutoCAD.DatabaseServices.BlockTable blockTable =

(Autodesk.AutoCAD.DatabaseServices.BlockTable)transaction.GetObject(

Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase.BlockTableId,

Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);

Autodesk.AutoCAD.DatabaseServices.BlockTableRecord blockTableRecord =

(Autodesk.AutoCAD.DatabaseServices.BlockTableRecord)transaction.GetObject(

blockTable[Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.ModelSpace],

Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);

foreach (Autodesk.AutoCAD.DatabaseServices.ObjectId objId in blockTableRecord)

{

objects.Add(objId);

}

transaction.Commit();

}

return objects;

}

#endregion

2、获取实体

#region "获取实体"

/// <summary>

/// 获取实体

/// </summary>

/// <param name="ObjectId"></param>

/// <returns>DBObject</returns>

private DBObject GetObject(ObjectId id)

{

DBObject obj = null;

using (Transaction transaction = Application.DocumentManager.

MdiActiveDocument.TransactionManager.StartTransaction())

{

obj = transaction.GetObject(id,

Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead, true);

transaction.Commit();

}

return obj;

}

#endregion

3、给实体添加XData

#region "如何给实体添加XData"

/// <summary>

/// 如何给实体添加XData

/// </summary>

/// <param name="DBObject,int"></param>

/// <returns>bool</returns>

public static bool AttachXData(DBObject obj, int MyData)

{

RegAppTableRecord app = new RegAppTableRecord();

app.Name = "云幽学院";

using (Transaction transaction =

Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())

{

try

{

Database workingdatabase = HostApplicationServices.WorkingDatabase;

SymbolTable table = (SymbolTable)transaction.GetObject(workingdatabase.RegAppTableId,

Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite, false);

if (!table.Has(app.Name))

{

table.Add(app);

transaction.AddNewlyCreatedDBObject(app, true);

}

if (!obj.IsWriteEnabled)

obj.UpgradeOpen();

obj.XData = new ResultBuffer(

new TypedValue((int)DxfCode.ExtendedDataRegAppName, app.Name),

new TypedValue((int)DxfCode.ExtendedDataInteger32, MyData));

transaction.Commit();

transaction.Dispose();

}

catch

{

return false;

}

}

return true;

}

#endregion

04-14 04:39