- 积分
- 367
- 明经币
- 个
- 注册时间
- 2011-9-25
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
程序的原理是这样:
在一个DWG文件中,有多张图纸。A3,A4等都有。将图框做成块。在程序中遍历所有图框块,找到每张图纸的位置,然后打印相应该的窗口。
这个算法我想来想去应该是没有问题。于是尝试着写了出来。主要代码如下。
using System;
....
using Autodesk.AutoCAD.PlottingServices;
namespace AllBlock
{
public class Class1
{
public static Document doc = Application.DocumentManager.MdiActiveDocument;
public Database db = doc.Database;
public Editor ed = doc.Editor;
public int index = 1;
[CommandMethod("ba")]
public void ba()
{
ObjectIdCollection objIdColl = GetSheet();
if (objIdColl == null)
{
return;
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (ObjectId objId in objIdColl)
{
BlockReference bref = tr.GetObject(objId, OpenMode.ForRead) as BlockReference;
Extents3d exSize = bref.GeometricExtents;
Point2d pStart = Point3dToPoint2d(exSize.MinPoint);
Point2d pEnd = Point3dToPoint2d(exSize.MaxPoint);
if (bref.Name == "a4") //区分A3和A4
{
int a = Plot(pStart, pEnd, bref.Rotation, 4);
}
else
{
int a = Plot(pStart, pEnd, bref.Rotation, 3);
}
}
}
}
private ObjectIdCollection GetSheet()
{}
private int Plot(Point2d pMin, Point2d pMax, double angle, int size)
{
//检查打印状态
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
//开启打印引擎
using (PlotEngine pe = PlotFactory.CreatePublishEngine())
{
/////....
}
}
return 0;
}
private Point2d Point3dToPoint2d(Point3d p3d)
{
}
}
}
本程序一共包含的1个类,4个函数。分别是:
public void ba(),
private ObjectIdCollection GetSheet()
private int Plot(Point2d pMin, Point2d pMax, double angle, int size)
private Point2d Point3dToPoint2d(Point3d p3d)
意思都很好理解。
现在出现了三个问题。
第一:文件中有多张图纸,GetSheet()函数也能将所有图纸全部选择,但无论有多少图纸,每次运行程序只能打印一页。经过多次测试发现关键在于 Plot()方法中的这一句:
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
.....
}
这句是在手册上操的,我尝试过去掉这句,但马上就有异常。不去掉这句,怎么也不能打印第二张图纸。
我还好现在我程序运行结束之后的几秒甚至十几秒之后,Autocad才开启发布作业。我本来的设想是只要运行一次Plot()就进行一次发布作业,这样有多少张图纸就有多少次发布作业。但实际情况和我所想的完全不同。是不是我的算法有问题?究竟能不能实现?
第二个问题是,虽然能够打印一张图纸,但打印出来的东西总是不对。刚开始测试的时候连续地打出白纸。后来怀疑是座标可能有冲突,重设为世界坐标系之后,也不对,虽然有时不再打白纸了。但也不知打印的是哪个区域,反正不是我想要的区域。想来起去真想不出来是为什么。
|
|