/// <summary>/// 区域打印pdf/// </summary>/// <param name="strPath">保存路径</param>/// <param name="minPt">最小点</param>/// <param name="maxPt">最大点</param>/// <param name="scale">缩放比例</param>public void Getdwfs(string strPath, Point3d minPt, Point3d maxPt, Scale3d scale){Document activeDoc = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;GcadDocument doc = activeDoc.AcadDocument as GcadDocument;doc.SetVariable("BACKGROUNDPLOT", 0);//前台打印doc.ActiveLayout.ConfigName = "DWG To PDF.pc3"; //"DWF6 ePlot.pc3";// // //使用的打印机设置名称 页面设置名称doc.ActiveLayout.StyleSheet = "monochrome.ctb";object LowerLeft = new double[2];//左下坐标,即xy最小值object UpperRight = new double[2];//右上坐标,即xy最大值((double[])LowerLeft)[0] = minPt.X;((double[])LowerLeft)[1] = minPt.Y;((double[])UpperRight)[0] = maxPt.X;((double[])UpperRight)[1] = maxPt.Y;doc.ActiveLayout.SetWindowToPlot(LowerLeft, UpperRight);doc.ActiveLayout.PlotType = GcPlotType.acWindow;//可以选择其他打印方式doc.ActiveLayout.PlotWithLineweights = true; //线宽比例doc.ActiveLayout.PlotWithPlotStyles = true; //使用样式doc.ActiveLayout.StandardScale = GcPlotScale.acScaleToFit; //自动缩放doc.ActiveLayout.UseStandardScale = true; // 使用标准比例doc.ActiveLayout.CenterPlot = true; // 居中doc.ActiveLayout.PlotRotation = GcPlotRotation.ac0degrees;//横向//0 横向,90 纵向,180反向,double Length = (maxPt.X - minPt.X) / scale.X;double Width = (maxPt.Y - minPt.Y) / scale.Y;//设置打印图纸大小string[] cmnamelst = (string[])doc.ActiveLayout.GetCanonicalMediaNames();foreach (string name in cmnamelst){string mediaName = doc.ActiveLayout.GetLocaleMediaName(name);//查找纸张大小if (mediaName.Contains("ISO full bleed") && mediaName.Contains(Length.ToString("f2")) && mediaName.Contains(Width.ToString("f2"))){if (mediaName.IndexOf(Length.ToString("f2")) < mediaName.IndexOf(Width.ToString("f2"))){doc.ActiveLayout.CanonicalMediaName = name;break;}}}doc.ActiveLayout.PaperUnits = GcPlotPaperUnits.acMillimeters; //单位毫米doc.Plot.DisplayPlotPreview(GcPreviewMode.acFullPreview);//预览doc.Plot.QuietErrorMode = true;//生成存档,避免报错string destPath = Path.GetDirectoryName(strPath) + "\\" + Path.GetFileNameWithoutExtension(strPath) + ".pdf";doc.Plot.PlotToFile(destPath);//第二个参数为打印机名称}
有时候打印为空,TARGET系统变量为0,0,0这种情况下,效果没有问题,但当TARGET不为0时,需要UCS->DCS的转换
[DllImport("gced.dll", CallingConvention = CallingConvention.Cdecl)]static extern private int gcedTrans(double[] point, IntPtr fromRb, IntPtr toRb, int disp, double[] result);//这段代码放在原先代码中的最前面Document acDoc = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;ViewTableRecord acView = acDoc.Editor.GetCurrentView();Point3d point = acView.Target;if (point.X == 0 && point.Y == 0 && point.Z == 0){}else { ResultBuffer rbF = new ResultBuffer();rbF.Add(new TypedValue(5003, 1));ResultBuffer rbT = new ResultBuffer();rbT.Add(new TypedValue(5003, 2));//ResultBuffer rbFrom = new ResultBuffer(new TypedValue(5003, 1));//ResultBuffer rbTo = new ResultBuffer(new TypedValue(5003, 2));double[] dMin = new double[] { 0, 0, 0 };double[] dMax = new double[] { 0, 0, 0 };gcedTrans(minPt.ToArray(), rbF.ResbufObject, rbT.ResbufObject, 0, dMin);gcedTrans(maxPt.ToArray(), rbF.ResbufObject, rbT.ResbufObject, 0, dMax);minPt = new Point3d(dMin[0], dMin[1], dMin[2]);maxPt = new Point3d(dMax[0], dMax[1], dMax[2]);}