ANYCAD-based DXF File Reading and conversion, anycaddxf reading

Source: Internet
Author: User

ANYCAD-based DXF File Reading and conversion, anycaddxf reading

The purpose of this article is to learn about ANYCAD and attach specific operations during this time, so that new users can take less detours.

I am a beginner in C # and have never been familiar with programming. I learned from my teacher during the summer. The first task was to open the DXF file with C #. I thought the task was very simple, BUT it is useless to search a lot on the Internet (for a newbie ), I accidentally saw an article titled how to quickly set up the reading, display, and conversion of DXF files to JPG, PNG, and PDF files based on AnyCAD.net. I learned about this ANYCAD control, however, when I entered the code in this article, I found that it was not very useful for a newbie. I could only say that it opened the door for me to learn.

AnyCAD Graphics SDK is a professional and comprehensive 3D Graphics application development kit. It is a CAD Platform Based on OpenCASCADE, it can be used to develop applications such as 2D/3D model design, computer simulation, and pre-and post-processing of finite element. According to the function module, the AnyCAD Graphics SDK Professional edition includes geometric modeling, 3D visualization and data exchange module. The Enterprise Edition adds the Data Management Module Based on the Professional Edition. AnyCAD Graphics SDK has two versions:. Net and C ++. the. Net version supports the WinForm and WPF interface frameworks, and the C ++ version supports interfaces such as MFC, Qt, and wxWidgets.

First, let's take a look at ANYCAD. SDK. This is a dynamic link library. For example, if you want to use the renderView in the code below, you must first define it for use. Where is the definition? Yes, it is in the SDK, so you must first download the anycad sdk. However, due to copyright issues, you only have to purchase the SDK after the trial period and the students have special care. I will not go into details here.

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         }

After you download the SDK (Note: Pay attention to the VS and SDK versions), the first step is to add a reference so that you can use the encapsulated function. The most important thing is to copy the added SDK file to your project.

Then add USING, such:

The following is all the code:

  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             else145             {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 }

Specific running effect:

 

I hope these things can help you, even if it is a bit, I am very happy!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.