I used the example here to side load dwgs into memory combined with the example here of Plot From Model Space. I got things working except for the plots are not respecting whether the layers are frozen or not and printing as if they were all thawed. The data coming in from the dwg is correct. I can iterate the layers while debugging and verify the correct ones are either frozen or thawed. Also If I just saveas the dwg to a new name it matches the original concerning the layer state.
[CommandMethod("PlotLayout")] public static void PlotLayout() { // Get the current document and database, and start a transaction Document acDoc = Active.Document; Database acCurDb = Active.Database; Editor ed = Active.Editor; var collection = new List<string>() { "C:\\Test\\440001A.dwg", "C:\\Test\\440001B.dwg", "C:\\Test\\440001C.dwg", "C:\\Test\\440001D.dwg" }; for (int i = 0; i < collection.Count; i++) { var dwg = collection[i]; var dir = Path.GetDirectoryName(dwg); var fn = Path.GetFileNameWithoutExtension(dwg); var filePath = Path.Combine(dir, fn + "-" + i.ToString() + ".pdf"); Database oldDb = HostApplicationServices.WorkingDatabase; using (Database db = new Database(false, true)) { db.ReadDwgFile(dwg, FileOpenMode.OpenForReadAndAllShare, false, null); db.CloseInput(true); using (Transaction acTrans = db.TransactionManager.StartTransaction()) try { HostApplicationServices.WorkingDatabase = db; LayoutManager acLayoutMgr = LayoutManager.Current; // Get the current layout and output its name in the Command Line window Layout acLayout = acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout), OpenMode.ForRead) as Layout; // Get the PlotInfo from the layout using (PlotInfo acPlInfo = new PlotInfo()) { acPlInfo.Layout = acLayout.ObjectId; // Get a copy of the PlotSettings from the layout using (PlotSettings acPlSet = new PlotSettings(acLayout.ModelType)) { acPlSet.CopyFrom(acLayout); // Update the PlotSettings object PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current; // Set the plot type acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents); // Set the plot scale acPlSetVdr.SetUseStandardScale(acPlSet, true); acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit); // Center the plot acPlSetVdr.SetPlotCentered(acPlSet, true); // Set the plot device to use acPlSetVdr.SetPlotConfigurationName(acPlSet, "DWF6 ePlot.pc3", "ANSI_B_(11.00_x_17.00_Inches)"); // Set the plot info as an override since it will // not be saved back to the layout acPlInfo.OverrideSettings = acPlSet; // Validate the plot info using (PlotInfoValidator acPlInfoVdr = new PlotInfoValidator()) { acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled; acPlInfoVdr.Validate(acPlInfo); // Check to see if a plot is already in progress if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting) { using (PlotEngine acPlEng = PlotFactory.CreatePublishEngine()) { // Track the plot progress with a Progress dialog using (PlotProgressDialog acPlProgDlg = new PlotProgressDialog(false, 1, true)) { using ((acPlProgDlg)) { // Define the status messages to display // when plotting starts acPlProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Plot Progress"); acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job"); acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet"); acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress"); acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress"); // Set the plot progress range acPlProgDlg.LowerPlotProgressRange = 0; acPlProgDlg.UpperPlotProgressRange = 100; acPlProgDlg.PlotProgressPos = 0; // Display the Progress dialog acPlProgDlg.OnBeginPlot(); acPlProgDlg.IsVisible = true; // Start to plot the layout acPlEng.BeginPlot(acPlProgDlg, null); // Define the plot output acPlEng.BeginDocument(acPlInfo, acDoc.Name, null, 1, true, "c:\\myplot"); // Display information about the current plot acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status, "Plotting: " + acDoc.Name + " - " + acLayout.LayoutName); // Set the sheet progress range acPlProgDlg.OnBeginSheet(); acPlProgDlg.LowerSheetProgressRange = 0; acPlProgDlg.UpperSheetProgressRange = 100; acPlProgDlg.SheetProgressPos = 0; // Plot the first sheet/layout using (PlotPageInfo acPlPageInfo = new PlotPageInfo()) { acPlEng.BeginPage(acPlPageInfo, acPlInfo, true, null); } acPlEng.BeginGenerateGraphics(null); acPlEng.EndGenerateGraphics(null); // Finish plotting the sheet/layout acPlEng.EndPage(null); acPlProgDlg.SheetProgressPos = 100; acPlProgDlg.OnEndSheet(); // Finish plotting the document acPlEng.EndDocument(null); // Finish the plot acPlProgDlg.PlotProgressPos = 100; acPlProgDlg.OnEndPlot(); acPlEng.EndPlot(null); } } } } } } } } catch (System.Exception ex) { ed.WriteMessage(ex.ToString()); } HostApplicationServices.WorkingDatabase = oldDb; } } }
Solved! Go to Solution.
Solved by gotMorris. Go to Solution.
Interesting. I have used layerstate files and Database.LayerStateManager in the past but that was using XREFs. This process sets up the viewport before plotting (WYSIWYG).
Layerstates are time consuming to create, but can be saved externally and shared.
No - I have not done this before. My guess is that Readdwg is not the same as XREF when it comes to plotting. This could mean that the doc in the editor is not in the same state as the doc in the application. But, if it looks correct in the editor, it should plot that way.
What's your goal - to open a list of dwg files and plot them? If that's the case, you don't need to insert an old database into a new database (Using Database db = new Database(false, true)).
You would use documentmanager and open. There should be several examples here. Something like:
DocumentCollection docmgr = Application.DocumentManager;
Document mydoc = docmgr.Add("a dwg file from your list");
docmgr.MdiActiveDocument = mydoc;
Database db = HostApplicationServices.WorkingDatabase;
I actually need to do this with accoreconsole and a script file. I tried using DocumentCollection to add the dwg but it crashed. Using the standard AutoCad UI it created the pdf but was blank.
I haven't used accoreconsole either. Hopefully someone will point you in the right direction.
Other reading?
Not ideal but I found a work around. If I iterate the LayerTable, check for frozen then make IsPlottable false it prints correctly. If a better soultion is available please let me know. Thanks.
LayerTable _LayerTable = acTrans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable; foreach (ObjectId _LayerTableId in _LayerTable) { LayerTableRecord _LayerTableRecord = (LayerTableRecord)acTrans.GetObject(_LayerTableId, OpenMode.ForWrite); if (_LayerTableRecord.IsFrozen) { _LayerTableRecord.IsPlottable = false; } }