cad.net teigha配置教程 脱离cad读取dwg的方法A

dwg是Autodesk(桌子)的自己的格式.

dxf是桌子和其他公司用组码形式交换的格式.

teigha是美国开放设计联盟ODA (Open Design Alliance)破解dwg格式的库.所以他们之前打了官司...然后居然暧昧了起来...

桌子会随着新发布的ACAD发布而更新dwg格式,而ODA就紧紧跟在后面并且同步更新...

所以说,现在中望和浩辰都用着ODA的技术来实现dwg的读取,除非大家不想用dwg格式,否则他们也是无可奈何,

谁叫我们没有中国开放设计联盟..........钱?

下面是配置teigha的方法,以及c#操作teigha的方法:

1、Teigha中DWG数据库结构:

扫描二维码关注公众号,回复: 11326974 查看本文章

 

经常用到的有TextStyleTable、LayerTable、LinetypeTable、BlockTable及其对应的TextStyleTableRecord、LayerTableRecord、LinetypeTableRecord、BlockTableRecord及Entity。

2、具体使用

2.1添加引用

新建项目

 配置管理器

添加x86 


   选择x86

这样才会有x86的Debug文件夹,解压放入进去:

  

TeiGhaForDrawing4.00.0.rar就要更改net框架为4.0

添加引用文件:
….\bin\x86\Debug\TD_Mgd_4.00_10.dll

这样就完成了配置文件了..............................................................................................................................................................................................

使用前应该添加TD_Mgd_3.03_9.dll或是其他版本类库,类库分为64位与32位,32位类库在64位系统上运行效果可能不太好。3.02版本及一下版本可能没有64位的类库。

下面忘记是谁写的代码了....其实这部分的代码和Autodesk的差不多啦....

//命名空间有:
//3.02及以下版本命名空间应将Teigha换为DWGdirect。
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Colors;
using Teigha.Export_Import;
using Teigha.GraphicsInterface;
using Teigha.GraphicsSystem;
using Teigha.Runtime;

// 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)保存格式
// Save12为.dwg     Save13为dxf
// 默认保存为dwg,可以不用指定。
SaveType pSavetype = SaveType.Save12;  

// (2)保存版本类型
// ACAD2010为vAC24;ACAD2007为vAC21;ACAD2004为vAC18;
// 很重要,保存时要用,版本过高时低版本AutoCAD不能打开。
DwgVersion dwgver = DwgVersion.vAC18;  
 
// (3)保存
pDb.SaveAs(filename, dwgver);
//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())
    {
        // 表对象(记录)添加到表之前必须命名
        // isShapeFile flag must also be set (if true) before adding the object
        // to the database.
        pStyle.Name = styleName;//必须设定
        pStyle.IsShapeFile = isShapeFile;//添加(记录)到数据库之前必须设定(如果为true)
 
        // Add the object to the table.添加对象(记录)到表
        styleId = pStyles.Add(pStyle);
 
        // 设置剩下的属性。(可以添加后设置也可以添加之前设置)
        pStyle.TextSize = textSize;
        pStyle.XScale = xScale;
        pStyle.PriorSize = priorSize;
        pStyle.ObliquingAngle = obliquing;
        pStyle.FileName = fileName;
        if (isShapeFile)
            pStyle.PriorSize = 22.45;
 
        if (!string.IsNullOrEmpty(ttFaceName))
            pStyle.Font = new FontDescriptor(ttFaceName, bold, italic, charset, 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 = "AnnoBlock";
        annoBlockId = blockTable.Add(btr);
 
        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))
{
    if (!File.Exists(Application.StartupPath + "\\BLOCKS\\" + blockname + ".dwg"))
    {
        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))
    {
        using (BlockTable bt = (BlockTable) pDb.BlockTableId.Open(OpenMode.ForWrite))
        {
            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 = new Vertex2d();//顶点
            Point3d pos = start;//起点
            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 = false;//此属性在画线时不加,但在成面时将属性变为true
            if (linestyle != 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 = new Scale3d(scale, scale, scale);//比例
    pBlkRef.LayerId = layerID;//图层id
    btr.AppendEntity(pBlkRef);//插入块
}

// 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))
        {
            ObjectId linetypeId = pLinetypes[linetype];
            pLayer.LinetypeObjectId = linetypeId;
        } 
        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;
    using (ViewportTableRecord vtr=(ViewportTableRecord) viewportid.GetObject(OpenMode.ForWrite))
    {
        Point2d center = new Point2d((X, Y);//定义中心点
        vtr.CenterPoint = center;
        vtr.Height = 105;//高度
        vtr.Width = 130;//宽度
    }
}
View Code

猜你喜欢

转载自www.cnblogs.com/JJBox/p/13169778.html