当前位置:软件学习 > AutoCAD >>

C#调用AutoCAD组件将DWG转成PDF的方法

下面就是个演示例子(目前我在HIGER所以版权是它的,呵呵),原理就是把DWG打印到一个打印机上,要想实现自动打印机要支持自动保存,下面的PDFCreator就支持。

想要调试就自己建一个项目加个FORM就好了~~~~~~~~~~~

\

 

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using Autodesk.AutoCAD.Interop; 
using System.IO; 
 
namespace WindowsFormsApplication1 

    public partial class Form1 : Form 
    { 
        AcadApplicationClass app; //常驻应用程序  
 
        public Form1() 
        { 
            InitializeComponent(); 
 
            try 
            { 
                app = new AcadApplicationClass(); 
            } 
            catch 
            { 
                MessageBox.Show("Start AutoCAD object failed! Installed?"); 
                button1.Enabled = false; 
                return; 
            } 
        } 
 
        private void button1_Click(object sender, EventArgs e) 
        { 
            if (!File.Exists(textBox1.Text)) 
            { 
                MessageBox.Show("File is not exists"); 
                return; 
            } 
 
            AcadDocument doc; 
            try 
            { 
                doc = app.Documents.Open(textBox1.Text, null, null); //打开文件  
            } 
            catch (Exception error) 
            { 
                MessageBox.Show("Can't open the file. " + error.Message); 
                return; 
            } 
             
            doc.ModelSpace.Layout.PlotType = Autodesk.AutoCAD.Interop.Common.AcPlotType.acExtents; //定义打印范围为自动延伸全图纸  
            doc.ModelSpace.Layout.ConfigName = textBox3.Text; //选定打印机  
            doc.ModelSpace.Layout.CanonicalMediaName = comboBox1.Text; //设置图幅  
 
            doc.Plot.NumberOfCopies = 1; //打印份数  
            try 
            { 
                doc.Plot.PlotToDevice(null); 
            } 
            catch (Exception error) 
            { 
                MessageBox.Show("Send to printer failed. " + error.Message); 
                return; 
            } 
            finally 
            { 
                doc.Close(false, null); 
            } 
        } 
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
        { 
            if (app != null) 
                try 
                { 
                    app.Quit(); 
                } 
                catch { } 
        }   www.zzzyk.com
 
        private void button2_Click_1(object sender, EventArgs e) 
        { 
            OpenFileDialog d = new OpenFileDialog(); 
            d.Filter = "AutoCAD图纸(*.dwg)|*.dwg"; 
            if (d.ShowDialog() == DialogResult.OK) 
            { 
                textBox1.Text = d.FileName; 
         &nb

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,