因为做Revit的二次开发多一点,拿到Teigha的时候还有点懵,看完样例还是觉得一知半解的,原因还是对于AutoCad的底层逻辑不是很明白,包括视图与布局之间的层级关系,块与视图的关系等等,看代码会有些累。
下面通过一个案例说一下昨天一天整理的知识点。
下面代码是我查询一个cad文件中Geometry信息的案例,包括从模型视图提取数据与从块中提取信息。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Runtime;
namespace RdFileLayer
{
class Program
{
static void Main(string[] args)
{
using (Services serv = new Services())
{
var path = @"C:\Users\Desktop\二次开发\Teigha_cad_test.dwg";
using (Database db = new Database(false,false))
{
db.ReadDwgFile(path,FileShare.Read,false,null);
using (LayerTable pTable = (LayerTable)db.LayerTableId.GetObject(OpenMode.ForRead))
{
foreach (ObjectId id in pTable)
{
using (var tr = db.TransactionManager.StartTransaction())
{
LayerTableRecord record = (LayerTableRecord) tr.GetObject(id, OpenMode.ForRead);
Console.WriteLine("-------");
Console.WriteLine("<" + record.GetRXClass().Name + ">");
Console.WriteLine(record.Name);
Console.WriteLine("In Use" + record.IsUsed);
Console.WriteLine("Color" + record.Color);
Console.WriteLine("LineWeight" + record.LineWeight);
}
}
}
using (var tr = db.TransactionManager.StartTransaction())
{
using (var bTable = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead))
{
foreach (ObjectId id in bTable)
{
var record = tr.GetObject(id, OpenMode.ForRead) as BlockTableRecord;
int count = 0;
// var objectId = bTable[BlockTableRecord.ModelSpace];
// BlockTableRecord record =
// (BlockTableRecord)tr.GetObject(objectId, OpenMode.ForRead);
Console.WriteLine("DB_BlockTableRecord Type is : "+record.Name);
if (record.Name == BlockTableRecord.ModelSpace|| record.Name == "Block")
{
Console.WriteLine("----------------" + record.Name + "----------------");
foreach (ObjectId id1 in record)
{
var entity = (Entity)tr.GetObject(id1, OpenMode.ForRead, false, true);
Console.WriteLine("Entity Type MutiType is : ");
Console.WriteLine("DB_Type : " + entity.GetType());
if (entity is Line)
{
var item = entity as Line;
Console.WriteLine("Line - " + count);
Console.WriteLine("Line 0" + item.StartPoint);
Console.WriteLine("Line 1" + item.EndPoint);
Console.WriteLine("Line Type Name" + item.Layer);
}
else if (entity is Polyline)
{
var item = entity as Polyline;
var allNums = item.NumberOfVertices;
Console.WriteLine("DB_PolyLine : ");
for (int i = 0; i < allNums; i++)
{
var subLine = item.GetLineSegmentAt(i);
Console.WriteLine("SubLine - " + i);
Console.WriteLine("SubLine 0" + subLine.StartPoint);
Console.WriteLine("SubLine 1" + subLine.EndPoint);
Console.WriteLine("SubLine Type Name" + item.Layer);
}
Console.WriteLine("DB_PolyLine Coordinates : " + "DB_PolyLine count is : " + item.NumberOfVertices);
for (int i = 0; i < item.NumberOfVertices; i++)
{
var vertex = item.GetPoint3dAt(i);
Console.WriteLine("SubVertex Coordinate index : " + i + " Location : " + vertex);
}
}
else if (entity is Circle cItem)
{
Console.WriteLine("DB_Circle original : " + cItem.Center);
Console.WriteLine("DB_Circle Radius is : " + cItem.Radius);
}
else if (entity is DBText item)
{
Console.WriteLine("DB_Text is :" + item.TextString);
Console.WriteLine("DB_Text Position : "+ item.Position);
}
else if (entity is MText mItem)
{
Console.WriteLine("DB_MText value is : " + mItem.Text);
Console.WriteLine("DB_MText position is : " + mItem.Location);
}
count++;
}
}
}
}
}
}
}
Console.Read();
}
}
}
08.25 嵌套快提取,一个车库中有时会将整个车库图层打成一个块车位单独一个块,当我们想提取车位块中的信息时,需要提取嵌套块。但是当我们提取嵌套块时坐标会发生变化变为块内坐标,所以需要进行坐标转换,才能得到真实坐标,所以提取所有数据信息最好还是从Model-Space开始 遍历,获得块参照进入后对坐标进行修正才可以,有几层嵌套可能会需要添加几层,这一点我没有测试。
code:
else if (entity is BlockReference bItem)
{
var angle = (bItem.Rotation * 180) / Math.PI;
Console.WriteLine("DB_BlockReference : " + bItem.Name);
Console.WriteLine("DB_BlockReference Location ; " + bItem.Position);
Console.WriteLine("DB_BlockReference Rotation : " + angle);
if (angle - 0.00 < 0.001)
{
var subTable =
bItem.Database.BlockTableId.GetObject(OpenMode.ForRead) as
BlockTable;
var fakeTable = (BlockTableRecord) tr.GetObject(bItem.BlockTableRecord,
OpenMode.ForRead);
Console.WriteLine("Fake_Table type name is : " + fakeTable.Name);
Console.WriteLine("Fake_Table type Location is : " + fakeTable.Origin);
foreach (ObjectId objectId in fakeTable)
{
var subTableRecord =
tr.GetObject(objectId, OpenMode.ForRead) as Entity;
Console.WriteLine("DB_Entity Type is : "+subTableRecord.GetType());
Console.WriteLine("SubRecord PolyLines Vertex is : ");
if (subTableRecord is Polyline pl)
{
for (int i = 0; i < pl.NumberOfVertices; i++)
{
var nPoint = new Point2d(pl.GetPoint2dAt(i).X+bItem.Position.X,pl.GetPoint2dAt(i).Y+bItem.Position.Y);
Console.WriteLine("polyLines vertex index is : " + nPoint);
}
}
// if (subTableRecord.Name.ToLower() == BlockTabl
// foreach (var id2 in subTableRecord)
// {eRecord.ModelSpace.ToLower())
// {
// var subEntity = tr.GetObject(id2, OpenMode.ForRead, false,
// false) as Entity;
// Console.WriteLine("DB_Entity Type is : "+entity.GetType());
// }
// }
}
}
}
08.26