因為有人問到,所以寫了個例子。具體的要求是從.NET(比如C#)里面調(diào)用AutoCAD ActiveX API實現(xiàn)后臺打印DWG文件為PDF文件,而且要把打印頁面的大小設(shè)置成和DWG視圖的頁面的大小一致。當(dāng)然除了ActiveX API,其它接口,比如ObjectARX和AutoCAD.NET API也支持打印并能實現(xiàn)上述功能的。不過我們今天就限定一下范圍,用一用ActiveX API,而且指定產(chǎn)品是AutoCAD 2010吧。
成都創(chuàng)新互聯(lián)公司服務(wù)項目包括德安網(wǎng)站建設(shè)、德安網(wǎng)站制作、德安網(wǎng)頁制作以及德安網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,德安網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到德安省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
執(zhí)行步驟:打開一個dwg文件,用netload加載下面代碼所在的.dll文件,再輸入命令plottest,就得到輸出結(jié)果(一個.pdf文件)。
要用到的參考:
AcDbMgd.dll;AcMgd.dll;AutoCAD 2010 Type Library;System.Windows.Forms; AutoCAD/ObjectDBX Common 18.0 Type Library.
VB.NET:
Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Interop.Common
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Autodesk.AutoCAD.Runtime.CommandMethod("Plottest") _
Public Sub PlotToPDF()
Dim activeDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim ThisDrawing As AcadDocument = CType(activeDoc.AcadDocument, AcadDocument)
Dim layout As AcadLayout = ThisDrawing.ActiveLayout
Dim MediaName As String = layout.CanonicalMediaName
If MediaName.Equals("") Then
activeDoc.Editor.WriteMessage("There is no media set for the active layout.")
Return
Else
activeDoc.Editor.WriteMessage(("The media for the active layout is: " + MediaName))
End If
Try
Dim oplot As AcadPlotConfiguration = ThisDrawing.PlotConfigurations.Add("PDF", layout.ModelType)
oplot.PaperUnits = AcPlotPaperUnits.acMillimeters
oplot.StyleSheet = "monochrome.ctb"
oplot.PlotWithPlotStyles = True
oplot.ConfigName = "DWG To PDF.pc3"
oplot.UseStandardScale = True
oplot.StandardScale = AcPlotScale.acScaleToFit
oplot.PlotType = AcPlotType.acExtents
oplot.CenterPlot = True
Dim oMediaNames As Object = layout.GetCanonicalMediaNames
Dim mediaNames As ArrayList = New ArrayList(CType(oMediaNames, String()))
For Each sName As String In mediaNames
If sName.Contains(MediaName) Then
oplot.CanonicalMediaName = sName
layout.CopyFrom(oplot)
layout.PlotRotation = AcPlotRotation.ac0degrees
layout.RefreshPlotDeviceInfo()
ThisDrawing.SetVariable("BACKGROUNDPLOT", 0)
ThisDrawing.Plot.QuietErrorMode = True
ThisDrawing.Plot.PlotToFile("c:/temp/d1.pdf", "DWG To PDF.pc3")
oplot.Delete()
oplot = Nothing
Return
End If
Next
Catch es As System.Exception
System.Windows.Forms.MessageBox.Show(es.ToString)
End Try
End Sub
C#:
using System;
using System.Collections;
using System.Collections.Specialized;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
// Define Command "plotTest"
[CommandMethod("plotTest")]
static public void PlotToPDF()
{
Document activeDoc = Application.DocumentManager.MdiActiveDocument;
AcadDocument ThisDrawing = activeDoc.AcadDocument as AcadDocument;
AcadLayout layout = ThisDrawing.ActiveLayout;
String MediaName = layout.CanonicalMediaName;
if (MediaName.Equals(""))
{
activeDoc.Editor.WriteMessage("There is no media set for the active layout.");
return;
}
else
{
activeDoc.Editor.WriteMessage("The media for the active layout is: " + MediaName);
}
try
{
AcadPlotConfiguration oplot = ThisDrawing.PlotConfigurations.Add("PDF", layout.ModelType);
oplot.PaperUnits = AcPlotPaperUnits.acMillimeters;
oplot.StyleSheet = "monochrome.ctb";
oplot.PlotWithPlotStyles = true;
oplot.ConfigName = "DWG To PDF.pc3";
oplot.UseStandardScale = true;
oplot.StandardScale = AcPlotScale.acScaleToFit;
oplot.PlotType = AcPlotType.acExtents;
oplot.CenterPlot = true;
Object oMediaNames = layout.GetCanonicalMediaNames();
ArrayList mediaNames = new ArrayList((string[])oMediaNames);
foreach (String sName in mediaNames)
{
if (sName.Contains(MediaName))
{
oplot.CanonicalMediaName = sName;
layout.CopyFrom(oplot);
layout.PlotRotation = AcPlotRotation.ac0degrees;
layout.RefreshPlotDeviceInfo();
ThisDrawing.SetVariable("BACKGROUNDPLOT", 0);
ThisDrawing.Plot.QuietErrorMode = true;
ThisDrawing.Plot.PlotToFile("c://temp//d1.pdf","DWG To PDF.pc3");
oplot.Delete();
oplot=null;
return;
}
}
}
catch (System.Exception es)
{
System.Windows.Forms.MessageBox.Show(es.ToString());
}
}
輸出結(jié)果:
Process.Start(“cad主程序的路徑”,“要打開文件的目錄”)
比如用記事本打開 c:\1.txt
Process.Start("C:\Windows\notepad.exe", "c:\1.txt")
你是要操作cad文件?網(wǎng)上有操作dxf或dwg格式的代碼,你看看dxf的格式吧,是明碼的。
簡單的點線這些好實現(xiàn),但要尺寸標(biāo)注或其它的就要麻煩些
Dim?ppr?As?PromptPointResult?=?ed.GetPoint("請選擇插入點:")
Dim?pt?As?Point3d?=?ppr.Value
utility.WriteToEditor(pt.ToString())
Dim?pidBlock?As?New?PIDBlock()
'自己定義的圖塊類,保存圖塊的路徑和名稱?
pidBlock.Name?=?"sample"
pidBlock.Path?=?blockPath??"b_sample.dwg"
Using?blkDb?As?New?Database(False,?True)
'read?drawing?
blkDb.ReadDwgFile(pidBlock.Path,?System.IO.FileShare.Read,?True,?Nothing)
blkDb.CloseInput(True)
Using?docLock?As?DocumentLock?=?doc.LockDocument()
'多文檔要先這樣,否則報至命錯誤?
Using?t?As?Transaction?=?doc.TransactionManager.StartTransaction()
'insert?it?as?a?new?block?
Dim?idBTR?As?ObjectId?=?doc.Database.Insert(pidBlock.Name,?blkDb,?False)
'create?a?ref?to?the?block?
Dim?bt?As?BlockTable?=?DirectCast(t.GetObject(doc.Database.BlockTableId,?OpenMode.ForRead),?BlockTable)
Dim?btr?As?BlockTableRecord?=?DirectCast(t.GetObject(bt(BlockTableRecord.ModelSpace),?OpenMode.ForWrite),?BlockTableRecord)
Using?bref?As?New?BlockReference(pt,?idBTR)
btr.AppendEntity(bref)
t.AddNewlyCreatedDBObject(bref,?True)
End?Using
t.Commit()
End?Using
End?Using
End?Using
轉(zhuǎn)換成位圖肯定是可以瀏覽的,WMF文件沒試過。
你可以將圖片以二進(jìn)制形式存儲在數(shù)據(jù)庫中,如果是SQL Server,對應(yīng)字段的類型應(yīng)該是image。
攔截窗口程序消息可以解決
參考 VB王國榮API講座 講消息的那章
幾個API就可以搞定