Previewing and plotting a single sheet in AutoCAD using .NET
来源:互联网 发布:python 输入 编辑:程序博客网 时间:2024/05/16 04:06
I'm learning as I go for much of this, so there are structural (although usually not functional) changes being made to the code as it develops. In this instance, for example, I've factored off common functionality needed by both previewing and plotting into a single helper function. This will no doubt evolve further (and change in structure) when I come to apply the principle to multi-sheet plotting later in the week.
Here's the C# code:
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.PlottingServices;
namespace PlottingApplication
{
public class PreviewCommands
{
[CommandMethod("simprev")]
static public void SimplePreview()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
// PlotEngines do the previewing and plotting
if (PlotFactory.ProcessPlotState ==
ProcessPlotState.NotPlotting)
{
// First we preview...
PreviewEndPlotStatus stat;
PlotEngine pre =
PlotFactory.CreatePreviewEngine(
(int)PreviewEngineFlags.Plot
);
using (pre)
{
stat =
PlotOrPreview(
pre,
true,
db.CurrentSpaceId,
""
);
}
if (stat == PreviewEndPlotStatus.Plot)
{
// And if the user asks, we plot...
PlotEngine ple =
PlotFactory.CreatePublishEngine();
stat =
PlotOrPreview(
ple,
false,
db.CurrentSpaceId,
"c://previewed-plot"
);
}
}
else
{
ed.WriteMessage(
"/nAnother plot is in progress."
);
}
}
static PreviewEndPlotStatus PlotOrPreview(
PlotEngine pe,
bool isPreview,
ObjectId spaceId,
string filename)
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
PreviewEndPlotStatus ret =
PreviewEndPlotStatus.Cancel;
Transaction tr =
db.TransactionManager.StartTransaction();
using (tr)
{
// We'll be plotting the current layout
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
spaceId,
OpenMode.ForRead
);
Layout lo =
(Layout)tr.GetObject(
btr.LayoutId,
OpenMode.ForRead
);
// We need a PlotInfo object
// linked to the layout
PlotInfo pi = new PlotInfo();
pi.Layout = btr.LayoutId;
// We need a PlotSettings object
// based on the layout settings
// which we then customize
PlotSettings ps =
new PlotSettings(lo.ModelType);
ps.CopyFrom(lo);
// The PlotSettingsValidator helps
// create a valid PlotSettings object
PlotSettingsValidator psv =
PlotSettingsValidator.Current;
// We'll plot the extents, centered and
// scaled to fit
psv.SetPlotType(
ps,
Autodesk.AutoCAD.DatabaseServices.PlotType.Extents
);
psv.SetUseStandardScale(ps, true);
psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
psv.SetPlotCentered(ps, true);
// We'll use the standard DWF PC3, as
// for today we're just plotting to file
psv.SetPlotConfigurationName(
ps,
"DWF6 ePlot.pc3",
"ANSI_A_(8.50_x_11.00_Inches)"
);
// We need to link the PlotInfo to the
// PlotSettings and then validate it
pi.OverrideSettings = ps;
PlotInfoValidator piv =
new PlotInfoValidator();
piv.MediaMatchingPolicy =
MatchingPolicy.MatchEnabled;
piv.Validate(pi);
// Create a Progress Dialog to provide info
// and allow thej user to cancel
PlotProgressDialog ppd =
new PlotProgressDialog(isPreview, 1, true);
using (ppd)
{
ppd.set_PlotMsgString(
PlotMessageIndex.DialogTitle,
"Custom Preview Progress"
);
ppd.set_PlotMsgString(
PlotMessageIndex.SheetName,
doc.Name.Substring(
doc.Name.LastIndexOf("//") + 1
)
);
ppd.set_PlotMsgString(
PlotMessageIndex.CancelJobButtonMessage,
"Cancel Job"
);
ppd.set_PlotMsgString(
PlotMessageIndex.CancelSheetButtonMessage,
"Cancel Sheet"
);
ppd.set_PlotMsgString(
PlotMessageIndex.SheetSetProgressCaption,
"Sheet Set Progress"
);
ppd.set_PlotMsgString(
PlotMessageIndex.SheetProgressCaption,
"Sheet Progress"
);
ppd.LowerPlotProgressRange = 0;
ppd.UpperPlotProgressRange = 100;
ppd.PlotProgressPos = 0;
// Let's start the plot/preview, at last
ppd.OnBeginPlot();
ppd.IsVisible = true;
pe.BeginPlot(ppd, null);
// We'll be plotting/previewing
// a single document
pe.BeginDocument(
pi,
doc.Name,
null,
1,
!isPreview,
filename
);
// Which contains a single sheet
ppd.OnBeginSheet();
ppd.LowerSheetProgressRange = 0;
ppd.UpperSheetProgressRange = 100;
ppd.SheetProgressPos = 0;
PlotPageInfo ppi = new PlotPageInfo();
pe.BeginPage(
ppi,
pi,
true,
null
);
pe.BeginGenerateGraphics(null);
ppd.SheetProgressPos = 50;
pe.EndGenerateGraphics(null);
// Finish the sheet
PreviewEndPlotInfo pepi =
new PreviewEndPlotInfo();
pe.EndPage(pepi);
ret = pepi.Status;
ppd.SheetProgressPos = 100;
ppd.OnEndSheet();
// Finish the document
pe.EndDocument(null);
// And finish the plot
ppd.PlotProgressPos = 100;
ppd.OnEndPlot();
pe.EndPlot(null);
}
// Committing is cheaper than aborting
tr.Commit();
}
return ret;
}
}
}
When you execute the SIMPREV command, you receive enter a "preview" mode, from where you can either cancel or plot. The trick was really in determining the button selected by the user, which we do by passing an appropriate object (of class PreviewEndPlotInfo) into the EndPage() function, to collect information on what the users selects. The next post will take this further, allowing the user to cycle through multiple sheets using "next" and "previous" buttons.
- Previewing and plotting a single sheet in AutoCAD using .NET
- Previewing and plotting multiple sheets in AutoCAD using .NET
- Plotting a window from AutoCAD using .NET
- Driving a multi-sheet AutoCAD plot using .NET
- Creating a multileader in AutoCAD using a jig from .NET
- Creating a spline-segment multileader in AutoCAD using .NET
- Driving a basic AutoCAD plot using .NET
- color vector using in plotting example points and lines between corresponding vertices
- Plotting Engineering and Scientific Data using Java
- Using GDI+ in a Single Document MFC Project to Rotate, Zoom, and Constraint an Image
- Screen Capturing a Form in .NET - Using GDI and GDI+
- Generating Koch fractals in AutoCAD using .NET - Part 1
- Generating Koch fractals in AutoCAD using .NET - Part 2
- Previewing Video using DirectShow
- Real-time plotting in Python and Matlab
- Using a modal .NET dialog to display AutoCAD object properties
- Using a modeless .NET dialog to display AutoCAD object properties
- Updating a specific attribute inside an AutoCAD drawing using .NET
- Adobe Reader : Invalid plugin detected
- Driving a multi-sheet AutoCAD plot using .NET
- NHibernate1.2 邦定 GridView 很是郁闷!!!!
- 如何快速杀死占用过多资源(CPU,内存)的数据库进程(来自eygle)
- 数字格式化
- Previewing and plotting a single sheet in AutoCAD using .NET
- Free webcast reminder: "AutoCAD: 10 easy ways to crash your AutoCAD addin"
- 计算机网络概论
- Previewing and plotting multiple sheets in AutoCAD using .NET
- 10天内让google和baidu收录您的网站
- IT服务管理系统设计思路
- sprintf函数的用法(转载)
- 在linux下配置JAVA环境
- 手工清除隐藏的病毒和木马