C#CAD二次开发之DWG打印成PDF,设置区域打印


/// <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]);


            }

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漫步的社会人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值