Hey everyone,
I was simply trying to convert a dwg file into a pdf format without opening AutoCAD or any other external DWG viewer. I followed the general outline from the .NET development guide (
http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html?url=WS1a9193826455f5ff2566ffd511ff6f8c7ca-33b0.htm,topicNumber=d0e50852) along with the help of another developer (
http://through-the-interface.typepad.com/through_the_interface/2007/10/previewing-and-.html
) but I’ve been bombarded with countless errors.
First and foremost, what would be the best format for opening an external file? The DocumentManger.Open() command or the Database.readDWGFile()?
Second, does the DocumentManager.MdiActiveDocument = previously opened file, work at establishing the “current” document? Every solution requires access the “current” document, so would this work?
And does the [CommandMethod("XXXX")] have any significance? If so, what would I use to print/convert a single DWG file? ‘Simplot?’
And lastly, what is the cause of the errors of the form “Autodesk.AutoCAD.ApplicationServices.XXXXX does not have an implementation”? I made sure the “Copy Local” feature of all the references was set to False.
Thanks for all the help!!
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.PlottingServices;
//using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using CAD_AS = Autodesk.AutoCAD.ApplicationServices;
namespace ......(ommitted)
{
class AutoCADToPDF : (abstract class ommitted)
{
public AutoCADToPDF()
{ }
[CommandMethod("simplot")]
public override string Convert(string filein)
{
//**FILE IN STRING ERROR CHECK HERE
//**Make a try/catch statement
//Open the drawing; read-only
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(filein, true);
//Establish as current, active document
CAD_AS.Application.DocumentManager.MdiActiveDocument = dwg;
//**Probably unecessary, but setting document to Active
CAD_AS.Document doc = CAD_AS.Application.DocumentManager.MdiActiveDocument;
//Initalize the editor
Editor edit = doc.Editor;
//Initalize the database by loading the drawing into it
Database db = doc.Database;
//Start a new transaction against the open drawing
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//Reference the Layout Manager
LayoutManager layman = LayoutManager.Current;
//Open layout as read-only
Layout layout = (Layout)tr.GetObject(layman.GetLayoutId(layman.CurrentLayout),OpenMode.ForRead);
//Create plot info for layout
PlotInfo plotInfo = new PlotInfo();
plotInfo.Layout = layout.ObjectId;
//Copy plot settings from layout
PlotSettings settings = new PlotSettings(layout.ModelType);
settings.CopyFrom(layout);
//Retrieve the current plot settings validator
PlotSettingsValidator setValidator = PlotSettingsValidator.Current;
//Set Plot type
setValidator.SetPlotType(settings, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
//Set the Plot Scale and center the plot
setValidator.SetUseStandardScale(settings, true);
setValidator.SetStdScaleType(settings, StdScaleType.ScaleToFit);
setValidator.SetPlotCentered(settings, true);
//Set the plotter and media
setValidator.SetPlotConfigurationName(settings, "DWG to PDF.pc3", "ANSI_A_(8.50 x 11.00 Inches)");
//Link plot settings to plot info
//**Override not needed?
plotInfo.OverrideSettings = settings;
PlotInfoValidator infoValidator = new PlotInfoValidator();
infoValidator.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
infoValidator.Validate(plotInfo);
//Check to see if a plot is already in progress
if(PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
{
throw new ConvertException("Cannot proceed, another plot is in progress !");
}
//Plot the document
using (PlotEngine engine = PlotFactory.CreatePublishEngine())
{
//Silence the plot dialogue
PlotProgressDialog dlg = new PlotProgressDialog(false, 1, true);
dlg.OnBeginPlot();
dlg.IsVisible = false;
//SPecify plot destination
engine.BeginPlot(dlg, null);
engine.BeginDocument(plotInfo, filein, null, 1, true, @"D:\");
//Plot the first sheet
PlotPageInfo pageInfo = new PlotPageInfo();
engine.BeginPage(pageInfo, plotInfo, true, null);
engine.BeginGenerateGraphics(null);
engine.EndGenerateGraphics(null);
//End plotting sheet
engine.EndPage(null);
engine.EndDocument(null);
//Finish plot
dlg.OnEndPlot();
engine.EndPlot(null);
}
}
return @"D:\";
return null;
} //End Convert
}
}