使用Teigha.net读取CAD的常用功能模块

Teigha中实体旋转

代码:

using (var trans = database.TransactionManager.StartTransaction())

{

Entity ent = trans.GetObject(entityId, OpenMode.ForWrite) asEntity;

if (ent != null )

{

Extents3d exts = ent.GeometricExtents;

Point3d poCenter = newPoint3d((exts.MinPoint.X + exts.MaxPoint.X) / 2, (exts.MinPoint.Y + exts.MaxPoint.Y) / 2, 0);

Matrix3d Matr4 = Matrix3d.Rotation((30 * Math.PI / 180),Vector3d.ZAxis, poCenter);

ent.TransformBy(Matr4);

}

trans.Commit();

}

//以实体的外接矩形的中心点为圆心旋转30度。

 

Teigha中跳转至显示视图跳转到某点,并以此为圆心显示内容

代码:

using (var trans = database.TransactionManager.StartTransaction())

{

Point3d EndPoint=new Point3d(0,0,0);

using (Teigha.GraphicsSystem.View pView = helperDevice.ActiveView)

{

pView.Dolly(EndPoint.X - pView.Position.X, EndPoint.Y - pView.Position.Y, 0);

}

trans.Commit();

}

if (helperDevice != null)

helperDevice.Update();

Invalidate();

 

Teigha 窗体内容的旋转

代码:

if (helperDevice != null)

{

using (Teigha.GraphicsSystem.View pView = BaseF.helperDevice.ActiveView)

{

//2为指定的角度

pView.Roll(2 * Math.PI / 180);

Invalidate();

}

helperDevice.Update();

helperDevice.Invalidate();

}

Invalidate();

 

 

Teigha 直接将块中内容导入其他块中(适用于不同dwg文件的导入)

代码:

Database databa = newDatabase(false, false);

databa.ReadDwgFile(@"C:\Users\admin\Desktop\安装略图.dwg", FileOpenMode.OpenForReadAndAllShare, false, "");

using (var trans = database.TransactionManager.StartTransaction())

{

BlockTable block = database.BlockTableId.GetObject(OpenMode.ForWrite) asBlockTable;

using (var tran = databa.TransactionManager.StartTransaction())

{

BlockTable blockold = databa.BlockTableId.GetObject(OpenMode.ForWrite) asBlockTable;

database.Insert("需要导入的块名称", databa, false);

database.Insert( "需要导入的块名称",重新创建一个新的名字, databa, false);

}

trans.Commit();

}

 

Teigha 获取文档中包含的字体样式(主要用于计算机不包含CAD字体时)

//修改字体后需要刷新内容,最好是来回切换布局!

代码:

List<string> StrList = newList<string>();

using (var trans = BaseF.database.TransactionManager.StartTransaction())

{

using (TextStyleTable txtstyles = (TextStyleTable)trans.GetObject(BaseF.database.TextStyleTableId, OpenMode.ForRead))

{

string exCADlow = .eot,.eot,.otf,.fon,.font,.ttf,.ttc,.woff,.woff2,.shx

foreach (ObjectId item in txtstyles)

{

using (TextStyleTableRecord txtse = (TextStyleTableRecord)trans.GetObject(item, OpenMode.ForRead))

{

if (exCADlow.Contains(Path.GetExtension(txtse.FileName).ToLower()) && Path.GetExtension(txtse.FileName).ToLower() != "")

{

StrList.Add(txtse.FileName);

}

 

 

}

}

}

 

Teigha 实现移动视图的效果

//根据按下和移动时的鼠标坐标移动窗体显示的CAD内容

代码:

///<summary>

///移动视图

///</summary>

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

privatevoid Viewer_UDLR(Point PoMove)

{

double dx = -(PoMove.X - MouseDowns.X);

double dy = -(PoMove.Y - MouseDowns.Y);

MouseDowns = PoMove;

using (DBObject pVpObj = Aux.active_viewport_id(database).GetObject(OpenMode.ForWrite))

{

AbstractViewportData pAVD = newAbstractViewportData(pVpObj);

Teigha.GraphicsSystem.View pView = pAVD.GsView;

Vector3d vec = newVector3d(dx, dy, 0.0);

vec = vec.TransformBy((pView.ScreenMatrix * pView.ProjectionMatrix).Inverse());

pView.Dolly(vec);

pAVD.SetView(pView);

pAVD.Dispose();

pVpObj.Dispose();

Invalidate();

}

 

}

 

Teigha 移动实体所在位置

代码:

///<summary>

///移动实体

///</summary>

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

privatevoid MoverEntity_view(Point PoMove)

{

Point3d Podown1 = toEyeToWorld(MouseDowns.X, MouseDowns.Y);

Point3d Pomove1= toEyeToWorld(PoMove.X, PoMove.Y);

 

double dx = Pomove1.X - Podown1.X;

double dy = Pomove1.Y - Podown1.Y;

MouseDowns = PoMove;

List<ObjectId> Listitem = newList<ObjectId>();

 

using (var trans = database.TransactionManager.StartTransaction())

{

foreach (ObjectId item in SelectID)

{

if (Listitem.FindIndex(s1 => s1 == item) == -1)

{

Listitem.Add(item);

using (Entity Ent = item.GetObject(OpenMode.ForWrite) asEntity)

{

PoDowns = newPoint3d(PoDowns.X + dx, PoDowns.Y + dy,0);

Point3d Pointnew = newPoint3d(dx, dy, 0);

Matrix3d mat = Matrix3d.Displacement(Pointnew.GetAsVector());

Ent.TransformBy(mat);

}

}

}

trans.Commit();

}

Listitem.Clear();

if (helperDevice != null)

helperDevice.Update();

posted @ 2020-01-17 14:55  C#爆破  阅读(3763)  评论(6编辑  收藏  举报