AcadDocument AcdDoc = new AcadDocument();
AcadDocument doc = null;
AcdDoc.Application.Visible = false;
doc = AcdDoc.Application.Documents.Open(strFName, true, null); //以只读方式打开该文件
Delay(5000); //延迟调用
List<object[]> ls_rec = new List<object[]>();
dynamic cadObjectaa = doc.ModelSpace;//模型视图
//dynamic cadObjectaa = doc.PaperSpace;//Layout视图
foreach (var item in doc.ModelSpace)
{
dynamic cadObject = item;
string Layername = cadObject.Layer;//获取layer名称
if (Layername == "图框")
{
dynamic acadMLine = cadObject;
double[] coordinates = null;
try
{
coordinates = acadMLine.Coordinates;
object LowerLeft = new double[2];//左下坐标,即xy最小值
object UpperRight = new double[2];//右上坐标,即xy最大值
((double[])LowerLeft)[0] = coordinates.Where((r, index) => index % 2 == 0).Min();
((double[])LowerLeft)[1] = coordinates.Where((r, index) => index % 2 == 1).Min();
((double[])UpperRight)[0] = coordinates.Where((r, index) => index % 2 == 0).Max();
((double[])UpperRight)[1] = coordinates.Where((r, index) => index % 2 == 1).Max();
if (calcDiagonal(LowerLeft, UpperRight) > minDiagonal)
{
ls_rec.Add(new object[] { LowerLeft, UpperRight });
}
}
catch (Exception)
{
}
}
}
//ls_rec去除重复值
for (int i = 0; i < ls_rec.Count; i++) //外循环是循环的次数
{
for (int j = ls_rec.Count - 1; j > i; j--) //内循环是 外循环一次比较的次数
{
object LowerLeft_i = new double[2];//左下坐标,即xy最小值
object UpperRight_i = new double[2];//右上坐标,即xy最大值
object LowerLeft_j = new double[2];//左下坐标,即xy最小值
object UpperRight_j = new double[2];//右上坐标,即xy最大值
LowerLeft_i = ((object[])(ls_rec[i]))[0];
UpperRight_i = ((object[])(ls_rec[i]))[1];
LowerLeft_j = ((object[])(ls_rec[j]))[0];
UpperRight_j = ((object[])(ls_rec[j]))[1];
if (((double[])LowerLeft_i)[0] == ((double[])LowerLeft_j)[0]
&& ((double[])LowerLeft_i)[1] == ((double[])LowerLeft_j)[1]
&& ((double[])UpperRight_i)[0] == ((double[])UpperRight_j)[0]
&& ((double[])UpperRight_i)[1] == ((double[])UpperRight_j)[1])
{
ls_rec.RemoveAt(j);
}
}
}
foreach (var item in ls_rec)
{
//必须包含此代码才能调用导出
doc.SetVariable("BACKGROUNDPLOT", 1);//前台打印:0前台 前台默认打开 1后台 前台默认不打开
//doc.Application.ZoomExtents();
doc.ActiveLayout.ConfigName = "DWG To PDF.pc3";//使用的打印机设置名称 页面设置名称
doc.ActiveLayout.StyleSheet = "xx.ctb";//调用样式
doc.ActiveLayout.SetWindowToPlot(item[0], item[1]);
doc.ActiveLayout.CanonicalMediaName = "ISO_A3_(420.00_x_297.00_MM)";//打印格式A3
doc.ActiveLayout.PlotType = AcPlotType.acWindow;
doc.ActiveLayout.StandardScale = AcPlotScale.acScaleToFit; //标准比例,需要UseStandardScale = true;
doc.ActiveLayout.UseStandardScale = true; // 使用标准比例
doc.ActiveLayout.CenterPlot = true; // 居中
doc.ActiveLayout.PaperUnits = AcPlotPaperUnits.acMillimeters; //单位毫米
doc.ActiveLayout.PlotWithLineweights = true; //线宽比例
doc.ActiveLayout.PlotWithPlotStyles = true; //使用样式
int itemRx = Convert.ToInt32(((double[])item[1])[0]);
int itemLx = Convert.ToInt32(((double[])item[1])[1]);
int itemRy = Convert.ToInt32(((double[])item[0])[0]);
int itemLy = Convert.ToInt32(((double[])item[0])[1]);
int w = Math.Abs(itemRx - itemLx);
int h = Math.Abs(itemRy - itemLy);
if (w > h)
{
doc.ActiveLayout.PlotRotation = AcPlotRotation.ac0degrees;
}
else
{
doc.ActiveLayout.PlotRotation = AcPlotRotation.ac90degrees;
}
doc.Plot.QuietErrorMode = false;//生成存档,避免报错
string destPath = foldPath + @"\" + pdfIndex + ".pdf";//路径
doc.Plot.PlotToFile(destPath);
}
项目背景
需要按图层特定名称导出PDF
本人先是用Aspose.CAD 做导出 但是无法拆图 而且很慢 但是它不用装CAD 就很强。
后来发现了 autocad二开的一些方法借用了 框表的导出方式进行细化
如果你能知道它框框的名称 那就会比直接根据是否块元素查询快很多
链接:c# autocad二次开发 dwg文件的框表框图导出pdf方便打印 dwg转pdf工具_newidea07的博客-CSDN博客
本文主要供新手学习和交流。
存在问题
1.不能铺满
最后附上一个导出效果吧