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吧.

版本問題

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中間....所以輸出路徑一定也是 bin\x86\Debug\

 

x64

然后將下載的 teigha 3.09.10  解壓放入進去x64文件夾,

重要!! 執行文件.exe 必須在這些 dll中間....所以輸出路徑一定也是 bin\x64\Debug\

 

想分離的話,我是沒有辦法的......

可能需要和ODA聯系,然后拿到一版源碼,然后再設置相對路徑,然后再重新編譯?

知道的評論說一下....

 

   

引用文件

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

 

3.09.10添加引用文件:

….\bin\x86\Debug\TD_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

 

 

(完)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM