大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
因为有人问到,所以写了个例子。具体的要求是从.NET(比如C#)里面调用AutoCAD ActiveX API实现后台打印DWG文件为PDF文件,而且要把打印页面的大小设置成和DWG视图的页面的大小一致。当然除了ActiveX API,其它接口,比如ObjectARX和AutoCAD.NET API也支持打印并能实现上述功能的。不过我们今天就限定一下范围,用一用ActiveX API,而且指定产品是AutoCAD 2010吧。
成都创新互联公司服务项目包括德安网站建设、德安网站制作、德安网页制作以及德安网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,德安网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到德安省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
执行步骤:打开一个dwg文件,用netload加载下面代码所在的.dll文件,再输入命令plottest,就得到输出结果(一个.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());
}
}
输出结果:
Process.Start(“cad主程序的路径”,“要打开文件的目录”)
比如用记事本打开 c:\1.txt
Process.Start("C:\Windows\notepad.exe", "c:\1.txt")
你是要操作cad文件?网上有操作dxf或dwg格式的代码,你看看dxf的格式吧,是明码的。
简单的点线这些好实现,但要尺寸标注或其它的就要麻烦些
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
转换成位图肯定是可以浏览的,WMF文件没试过。
你可以将图片以二进制形式存储在数据库中,如果是SQL Server,对应字段的类型应该是image。
拦截窗口程序消息可以解决
参考 VB王国荣API讲座 讲消息的那章
几个API就可以搞定