寫這篇文章的目的是我這段時間關於ANYCAD的一些學習感悟,並且附上具體的操作,使新手少走一些彎路。
本人是C#的新手,從來沒接觸過編程,暑期跟著老師做學習,第一個任務就是用C#打開DXF文件,我以為任務很簡單,BUT 在網上搜索一大堆,都是沒用的(對於我這個新手來講),偶然一次看到一篇《基於AnyCAD.net 三維圖形平台快速搭建DXF文件的讀取、顯示、轉換成JPG, PNG,以及PDF》,了解了這個ANYCAD這個控件,但是這篇文章在我把代碼輸進去操作的時候發現對於我這個新手來說沒太大用,只能說它給我打開了一個學習的大門。
AnyCAD 三維圖形控件(AnyCAD Graphics SDK)是專業的、綜合的三維圖形應用開發包,是一個基於 OpenCASCADE 的CAD平台, 可以用於開發二維/三維模型設計、計算機仿真、有限元前後處理等應用程序。按照功能模塊,AnyCAD Graphics SDK 專業版包括幾何造型、三維可視化和數據交換模 塊,企業版在專業版的基礎上增加數據管理模塊。 AnyCAD Graphics SDK 擁有.Net 和C++兩個版本,其中.Net 版本支持WinForm 和WPF界面框架;C++版本支持MFC/Qt/wxWidgets 等界面庫。
首先了解一下ANYCAD.SDK,這是一個動態鏈接庫,舉個例子,你要用到下邊代碼中的renderView,那你首先得對它定義才能使用,定義在哪呢?對,就在SDK中,所以首先得下載ANYCAD的SDK,但是由於版權問題,只有試用期,過後得購買,學生有特殊照顧,這裡就不詳細說啦。
19 public MainForm() 20 { 21 InitializeComponent(); 22 23 this.renderView = new AnyCAD.Presentation.RenderWindow3d(); 24 this.renderView.Size = this.panel3d.ClientSize; 25 this.renderView.TabIndex = 1; 26 panel3d.Controls.Add(this.renderView); 27 }
當你下載SDK後(注:需要注意VS和SDK的版本),第一步是添加引用,這樣才能夠使用封裝的函數。最重要的是把添加的SDK文件復制到你的項目中。
然後添加USING,如下圖:
下面是全部的代碼:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using AnyCAD.Platform; 10 11 namespace DxfViewer 12 { 13 public partial class MainForm : Form 14 { 15 private AnyCAD.Presentation.RenderWindow3d renderView = null; 16 private ElementId mBeginId = new ElementId(); 17 private ElementId mEndId = new ElementId(); 18 19 public MainForm() 20 { 21 InitializeComponent(); 22 23 this.renderView = new AnyCAD.Presentation.RenderWindow3d(); 24 this.renderView.Size = this.panel3d.ClientSize; 25 this.renderView.TabIndex = 1; 26 panel3d.Controls.Add(this.renderView); 27 } 28 29 private void MainForm_Load(object sender, EventArgs e) 30 { 31 renderView.ShowWorkingGrid(false); 32 renderView.ExecuteCommand("ShadeWithEdgeMode"); 33 renderView.ShowCoordinateAxis(false); 34 35 ColorValue clr = new ColorValue(33f / 255f, 40f / 255f, 48f / 255f, 1); 36 renderView.SetBackgroundColor(clr, clr, clr); 37 38 Renderer renderer = renderView.Renderer; 39 40 // Customize the Axis 41 ScreenWidget coodinateNode = new ScreenWidget(); 42 AxesWidget axesNode = new AxesWidget(); 43 axesNode.SetArrowText((int)EnumAxesDirection.Axes_Z, ""); 44 coodinateNode.SetNode(axesNode); 45 coodinateNode.SetWidgetPosition(0); 46 renderer.SetCoordinateWidget(coodinateNode); 47 48 // Set the fixed Top View 49 renderer.SetStandardView(EnumStandardView.SV_Top); 50 renderer.SetViewType(EnumStandardView.SV_Top); 51 52 this.renderView.RequestDraw(); 53 54 } 55 56 private void MainForm_Resize(object sender, EventArgs e) 57 { 58 if (renderView != null) 59 renderView.Size = this.panel3d.ClientSize; 60 } 61 62 private void openToolStripMenuItem_Click(object sender, EventArgs e) 63 { 64 OpenFileDialog dlg = new OpenFileDialog(); 65 dlg.Filter = "DXF (*.dxf)|*.dxf"; 66 67 if (DialogResult.OK == dlg.ShowDialog()) 68 { 69 AnyCAD.Exchange.DxfReader reader = new AnyCAD.Exchange.DxfReader(); 70 renderView.ClearScene(); 71 AnyCAD.Exchange.ShowShapeReaderContext context = new AnyCAD.Exchange.ShowShapeReaderContext(renderView.SceneManager); 72 context.NextShapeId = mBeginId; 73 if (reader.Read(dlg.FileName, context, false)) 74 { 75 renderView.RequestDraw(); 76 mEndId = context.NextShapeId; 77 } 78 79 } 80 81 renderView.View3d.FitAll(); 82 } 83 84 private void pDFToolStripMenuItem_Click(object sender, EventArgs e) 85 { 86 SaveFileDialog dlg = new SaveFileDialog(); 87 dlg.Filter = "PDF (*.pdf)|*.pdf"; 88 if (DialogResult.OK == dlg.ShowDialog()) 89 { 90 renderView.Renderer.Print(dlg.FileName); 91 } 92 93 } 94 95 private void imageToolStripMenuItem_Click(object sender, EventArgs e) 96 { 97 SaveFileDialog dlg = new SaveFileDialog(); 98 dlg.Filter = "Image File (*.jpg;*.png)|*.jpg;*.png"; 99 if (DialogResult.OK == dlg.ShowDialog()) 100 { 101 renderView.CaptureImage(dlg.FileName); 102 } 103 } 104 105 private void dToolStripMenuItem_Click(object sender, EventArgs e) 106 { 107 renderView.ExecuteCommand("IsoView"); 108 renderView.View3d.FitAll(); 109 } 110 111 private void exportIgesToolStripMenuItem_Click(object sender, EventArgs e) 112 { 113 SceneManager sceneManager = renderView.SceneManager; 114 115 116 117 TopoShapeGroup group = new TopoShapeGroup(); 118 for (ElementId ii = mBeginId; ii < mEndId; ++ii) 119 { 120 SceneNode node = sceneManager.FindNode(ii); 121 if (node != null) 122 { 123 TopoShape shape = GlobalInstance.TopoShapeConvert.ToTopoShape(node); 124 if (shape != null) 125 { 126 Matrix4 trf = GlobalInstance.MatrixBuilder.MakeRotation(90, Vector3.UNIT_X); 127 shape = GlobalInstance.BrepTools.Transform(shape, trf); 128 group.Add(shape); 129 } 130 } 131 } 132 133 if (group.Size() > 0) 134 { 135 SaveFileDialog dlg = new SaveFileDialog(); 136 dlg.Filter = "IGES File (*.igs;*.iges)|*.igs;*.iges"; 137 if (DialogResult.OK != dlg.ShowDialog()) 138 { 139 return; 140 } 141 TopoDataExchangeIges igsWriter = new TopoDataExchangeIges(); 142 igsWriter.Write(group, new AnyCAD.Platform.Path(dlg.FileName)); 143 } 144 else 145 { 146 MessageBox.Show("No shape to save!"); 147 } 148 149 } 150 151 private void dToolStripMenuItem1_Click(object sender, EventArgs e) 152 { 153 renderView.ExecuteCommand("TopView"); 154 this.renderView.RequestDraw(); 155 } 156 157 } 158 }
具體運行效果:
希望我的這些東西可以幫到你哪怕一點,我也是很開心的!