logo资料库

Teigha使用说明.pdf

第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
资料共9页,剩余部分请下载后查看
1、 Teigha 中 DWG 数据库结构: 经 常 用 到 的 有 TextStyleTable 、 LayerTable 、 LinetypeTable 、 BlockTable 及 其 对 应 的 TextStyleTableRecord 、LayerTableRecord、LinetypeTableRecord 、BlockTableRecord 及 Entity 。 2、具体使用 2.1 添加引用 使用前应该添加 TD_Mgd_3.03_9.dll 或是其他版本类库, 类库分为 64 位与 32 位,32 位类库 在 64 位系统上运行效果可能不太好。 3.02 版本及一下版本可能没有 64 位的类库。 命名空间有: Teigha.DatabaseServices; Teigha.Geometry; Teigha.Colors; Teigha.Export_Import; Teigha.GraphicsInterface; Teigha.GraphicsSystem; Teigha.Runtime; 3.02 及以下版本命名空间应将 Teigha 换为 DWGdirect 。
2.2 打开、新建、保存数据库 使用之前应加上这个: using ( Services ser = new Services ())// 一个应用程序加上一个就行了,否则出错 1、打开数据库( dwg 文件) using ( Database pDb = new Database( false , false ))// 不加参数会出错 { pDb.ReadDwgFile( Application .StartupPath + "\\TABMENU.dwg", FileOpenMode.OpenForReadAndWriteNoShare, false , "" ); } 2、新建数据库 using ( Database pDb = new Database())// 加参数出错 3、保存 (1)保存格式 SaveType pSavetype = SaveType.Save12; 默认保存为 dwg ,可以不用指定。 (2)保存版本类型 DwgVersion dwgver = DwgVersion.vAC18; vAC18; 很重要,保存时要用,版本过高时低版本 (3)保存 pDb.SaveAs(filename, dwgver); //Save12 为.dwg Save13 为dxf //ACAD2010为 vAC24; ACAD2007为 vAC21; ACAD2004为 AutoCAD 不能打开。 pDb 为数据库( Database), filename 为 dwg 文件名, dwgver 为版本。 2.3 写数据 2.3.1 添加文本样式 ObjectId styleId = ObjectId .Null; using ( TextStyleTable pStyles = ( TextStyleTable )pDb.TextStyleTableId.GetObject( OpenMode.ForWrite)) { { // 文本样式记录 using ( TextStyleTableRecord pStyle = new TextStyleTableRecord ()) // 表对象(记录)添加到表之前必须命名 // flag must also be set (if isShapeFile true) before adding the object
pStyle.Name = styleName; // 必须设定 // to the database. pStyle.IsShapeFile = isShapeFile; // 添加(记录)到数据库之前必须设定 (如果为 true ) styleId = pStyles.Add(pStyle); // Add the object to the table. 添加对象(记录)到表 // 设置剩下的属性。 (可以添加后设置也可以添加之前设置) pStyle.TextSize = textSize; pStyle.XScale = xScale; pStyle.PriorSize = priorSize; pStyle.ObliquingAngle = obliquing; pStyle.FileName = fileName; (isShapeFile) pStyle.PriorSize = 22.45; if pStyle.Font = newFontDescriptor (ttFaceName, bold, italic, charset, if (! string .IsNullOrEmpty(ttFaceName)) pitchAndFamily); return styleId; } } 注: pDb 为 Database 2.3.2 添加线型 using ( LinetypeTable pLinetypes = ( LinetypeTable )pDb.LinetypeTableId.GetObject( OpenMode.ForWrite)) { { // 线表记录 using ( LinetypeTableRecord pLinetype = new LinetypeTableRecord ()) pLinetype.Name = name; // 必须命名 ObjectId linetypeId = pLinetypes.Add(pLinetype); // 添加记录 return linetypeId; } }
注:线型要有相应的线型文件,且不一定能够加成功,线型可以在使用之前手动加在 模板中,从其他文件向 dwg 文件复制线型,可能不成功。 dwg 2.3.3 添加块 例: using ( BlockTable blockTable = ( BlockTable )pDb.BlockTableId.GetObject( OpenMode.ForWrite)) { { ObjectId annoBlockId; using ( BlockTableRecord btr = new BlockTableRecord ()) btr.Name = annoBlockId = blockTable.Add(btr); "AnnoBlock" ; using ( Circle pCircle = new Circle ()) { pCircle.SetDatabaseDefaults(pDb); btr.AppendEntity(pCircle); Point3d center = new Point3d (0, 0, 0); pCircle.Center = center; pCircle.Radius = 0.5; } } } 向块表中加入块之前, 块一定要有名字。 同时可以从其他文件中提取块, 加入到目标数据库 中 例: using ( Database db = new Database( false , false )) { ".dwg" )) { } if (! File .Exists( Application .StartupPath + "\\BLOCKS\\" + blockname + MessageBox.Show(" 没找到 CASS块文件 " ); return ObjectId .Null; db.ReadDwgFile( Application .StartupPath + "\\BLOCKS\\" + blockname + ".dwg" , FileOpenMode.OpenForReadAndAllShare, false , "" ); using ( BlockTable pTable = ( BlockTable )db.BlockTableId.Open( OpenMode.ForRead)) { ( BlockTable )pDb.BlockTableId.Open( OpenMode.ForWrite)) using ( BlockTable bt = {
using ( BlockTableRecord btr = new BlockTableRecord ()) { { foreach ( ObjectId id in pTable) using ( BlockTableRecord pBlock = ( BlockTableRecord )id.Open( OpenMode.ForRead)) { { foreach ( ObjectId entid in pBlock) using ( Entity pEnt = ( Entity )entid.Open( OpenMode.ForRead, { false , true )) btr.AppendEntity(( Entity )pEnt.Clone()); } } } } btr.Name = blockname; ObjectId blockid = bt.Add(btr); return blockid; } } } } 2.3.4 向模型( model)空间画实体(线、面等) 加入线,例: using ( BlockTable blockTable = ( BlockTable )pDb.BlockTableId.GetObject( OpenMode.ForWrite)) { ObjectId modelSpaceID = blockTable[ BlockTableRecord .ModelSpace]; using ( BlockTableRecord btr = ( BlockTableRecord )modelSpaceID.GetObject( OpenMode.ForWrite)) { { using ( Polyline2d pline = new Polyline2d ()) btr.AppendEntity(pline);// 向块记录中添加线 Vertex2d pVertex = Point3d pos = start;// new Vertex2d ();// 顶点 起点 pVertex = new Vertex2d (); pline.AppendVertex(pVertex);
pos = start;// pVertex.Position = pos; 起点 pVertex.Dispose(); pVertex = new Vertex2d (); pline.AppendVertex(pVertex); pos = end;// 顶点,终点 pVertex.Position = pos; if (linewidth >= 0.0) { pVertex.StartWidth = linewidth; pVertex.EndWidth = linewidth; } pVertex.Dispose(); // 线宽 //pline.Closed (linestyle != if = false;// 此属性在画线时不加, 但在成面时将属性变为 true null ) { pline.Linetype = linestyle;// } 线型 pline.Layer = LayerName;// 图层名 } } } } 加入面的操作与上面加入线类似,但最后线的 Closed 属性应设置成 true。 插入文字: using ( BlockTableRecord bBTR = ( BlockTableRecord )modelSpaceID.GetObject( OpenMode.ForWrite)) { { using ( DBText pText = new DBText()) // 开始时插入文字以左上点为准插入 using ( Database pDb = bBTR.Database) pText.SetDatabaseDefaults(pDb); ObjectId textId = bBTR.AppendEntity(pText); // 注释 pText.Annotative = AnnotativeStates .True; // 加入到特殊群 if (pGroup != null ) pGroup.Append(textId);
pText.Position = position; // 位置(应该是左上方) pText.AlignmentPoint = alignmentPoint; // 校准点什么东西 pText.Height = height; pText.WidthFactor = 1.0; pText.TextString = text; // 高度 // 什么东西 pText.HorizontalMode = hMode; pText.VerticalMode = vMode; pText.Oblique = OdaToRadian(oblique); pText.Rotation = OdaToRadian(rotation); // 文字模式 // 垂直模式 // 倾斜 // 旋转 // 文字样式 if (!textstyleID.IsNull) pText.TextStyleId = textstyleID; // 层名 if (!layerId.IsNull) pText.SetLayerId(layerId, false ); { if (widthfactor != 0.0) pText.WidthFactor = widthfactor;// 宽度因子 } } } 插入块: using ( BlockTableRecord btr = ( BlockTableRecord )modelSpaceID.GetObject( OpenMode.ForWrite)) { BlockReference pBlkRef = new BlockReference (point, btr.ObjectId);//point 为插入的位置 pBlkRef.BlockTableRecord = BlockRefID;// 块在数据库中的 id pBlkRef.ScaleFactors = pBlkRef.LayerId = layerID;// btr.AppendEntity(pBlkRef);// new Scale3d (scale, scale, scale);// 比例 图层 id 插入块 } 2.3.5 图层 加入图层: using ( LayerTable pLayers = ( { LayerTable )pDb.LayerTableId.GetObject( OpenMode.ForWrite))
// 图层 using ( LayerTableRecord pLayer = new LayerTableRecord ()) { pLayer.Name = name; // 图层名必须有 pLayer.Color = Color .FromColorIndex( ColorMethod .ByAci, color); // 颜色 // 图层线型 using ( LinetypeTable pLinetypes = ( LinetypeTable )pDb.LinetypeTableId.GetObject( OpenMode.ForWrite)) { pLayer.LinetypeObjectId = linetypeId; ObjectId linetypeId = pLinetypes[linetype]; return pLayers.Add(pLayer);//ObjectID } } } 2.4 关于视图 在写完 dwg 文件之后, 打开 dwg 有时会找不到所画的实体, 因此在保存前应先定义好视图: 方法一: using ( ViewportTable vt = ( ViewportTable )pDb.ViewportTableId.GetObject( OpenMode.ForWrite)) { { foreach ( ObjectId item in vt) using ( ViewportTableRecord vtr = ( ViewportTableRecord )item.GetObject( OpenMode.ForWrite)) { Point2d center = new Point2d ((X ,Y);// 定义中心点 vtr.CenterPoint = center; vtr.Height = 105;// vtr.Width = 130; 高度 宽度 } } } 方法二: LayoutManager lm = LayoutManager .Current; ObjectId layoutid = lm.GetLayoutId(lm.CurrentLayout);//lm.CurrentLayout 新建 时默认为” Model”,当其改变时应换为” Model”的名字 using ( Layout pLayout=( Layout )layoutid.GetObject( OpenMode.ForWrite)) { ObjectId viewportid = pLayout.CurrentViewportId;
分享到:
收藏