zoukankan      html  css  js  c++  java
  • cad.net 脱离cad读取dwg的方法A-TeiGhaForDrawing配置教程

    简述

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

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

    TeiGhaForDrawing (后面都会简称teigha) 是美国开放设计联盟ODA (Open Design Alliance)破解dwg格式的库.所以他们之前打了官司...

    然后桌子弹出 "非AutodeskDWG。此DWG文件由非Autodesk开发或许可的软件应用程序保存。将此文件与AutoCAD软件一起使用可能导致稳定性问题。"

    这么打着打着,到了如今他们居然暧昧了起来...不强行弹窗了...

    桌子会随着新发布的ACAD发布而更新.dwg格式(就是大家熟知的07版格式,10版格式等等...),而ODA就紧紧跟在后面并且同步更新...

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

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

    这里并不提供teigha的下载链接(为了文章永存)...大家自行去对应的网络,QQ群,等等问问其他前辈要就有了...

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

    Teigha中DWG数据库结构

     

    经常用到的有TextStyleTable、LayerTable、LinetypeTable、BlockTable及其对应的

    TextStyleTableRecord、LayerTableRecord、LinetypeTableRecord、BlockTableRecord

    及Entity,几乎和桌子的一模一样,不过这个玩意是没有内置的打印函数的,毕竟只是解析.dwg的内库.

    新建项目

    本此展示是一个控制台程序,当然了,你也可以去用winform,WPF来制作一个具有界面的cad.

    在vs上面新建一个net framework吧.

    TeiGhaForDrawing 版本问题: 

    3.09.10为    x86不可. x64可用 (亲测)

    4.00.0 为    x86可用. x64不可 (亲测)

    非亲测指引:

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

    3.02版本及以下版本可能没有64位的类库.

    群里来的一张图:

        

    都可以用 net 4.0 框架:

    然后配置管理器,如果没有就添加x86.

     

    选择x86添加.

      

    注意下面如图设置:

    X86

    配置新建里x86,这样才会有 x86的Debug 文件夹,

    然后将下载的 teigha 4.00.0 解压放入进去x86文件夹,

    重要!! 执行文件.exe 必须在这些 dll中间....所以输出路径一定也是 binx86Debug

    x64

    然后将下载的 teigha 3.09.10  解压放入进去x64文件夹,

    重要!! 执行文件.exe 必须在这些 dll中间....所以输出路径一定也是 binx64Debug

    想分离的话,我是没有办法的......

    可能需要和ODA联系,然后拿到一版源码,然后再设置相对路径,然后再重新编译?

    知道的评论说一下....

       

    引用文件

    4.00.0添加引用文件:
    ….inx86DebugTD_Mgd_4.00_10.dll

    3.09.10添加引用文件:

    ….inx86DebugTD_Mgd.dll

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

    代码

    这部分的代码和桌子的差不多.

    //3.02及以下版本命名空间应将Teigha换为DWGdirect
    #if Teigha302Dnow
    using DWGdirect.DatabaseServices;
    using DWGdirect.Geometry;
    using DWGdirect.Colors;
    using DWGdirect.Export_Import;
    using DWGdirect.GraphicsInterface;
    using DWGdirect.GraphicsSystem;
    using DWGdirect.Runtime; 
    #else
    using Teigha.DatabaseServices;
    using Teigha.Geometry;
    using Teigha.Colors;
    using Teigha.Export_Import;
    using Teigha.GraphicsInterface;
    using Teigha.GraphicsSystem;
    using Teigha.Runtime;
    #endif
    
    // 参考的Teigha文件
    // https://wenku.baidu.com/view/1019d1850b1c59eef9c7b41f.html?tdsourcetag=s_pcqq_aiomsg&qq-pf-to=pcqq.group
    // 参考的错误
    // https://jingyan.baidu.com/article/bea41d439008dcb4c51be60b.html
    
    namespace JJBoxTeigha
    {
        public class Program
        {
            static void Main(string[] args)
            {
                //打开、新建、保存数据库
                //处理数据库之前,需要启用Teigha的服务程序.一个应用程序加上一个就行了,否则出错
                using (Services ser = new Services())
                {
                    //打开数据库(dwg文件)
                    using (Database pDb = new Database(false, false))//不加参数会出错
                    {
                        pDb.ReadDwgFile(Application.StartupPath + "\TABMENU.dwg", FileOpenMode.OpenForReadAndWriteNoShare, false, "");
                        //pDb取得了,再加入你想处理的代码......
                    }
    
                    //新建数据库
                    using (Database pDb = new Database())//加参数出错
                    {
                        //pDb取得了,再加入你想处理的代码......
    
                        // 保存数据库
                        // A.保存格式
                        // Save12为.dwg,Save13为dxf
                        // 默认保存为dwg,可以不用指定。
                        SaveType pSavetype = SaveType.Save12;
    
                        // B.保存版本类型
                        // ACAD2010为vAC24;ACAD2007为vAC21;ACAD2004为vAC18;
                        // 很重要,保存时要用,版本过高时低版本AutoCAD不能打开。
                        DwgVersion dwgver = DwgVersion.vAC18;
    
                        // C.保存语句
                        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

    (完)

  • 相关阅读:
    90后是怎么了
    从GNOME切换到KDE了
    Ubuntu 12.04中安装Evolus Pencil原型图绘制软件
    wine qq 2012 for linux
    发现来博客园比去csdn早
    [转]代理(Proxy)和委派(Delegate)的区别
    Debian Stable分支对于开发者的意义[续软件系统。。。]
    xj3d svn org.web3d目录结构分析
    不自觉的就陷入OS发行版选择的泥潭
    DNN Test
  • 原文地址:https://www.cnblogs.com/JJBox/p/13169778.html
Copyright © 2011-2022 走看看