publisher.PublishExecute gives Fatal error unhandled access violation

publisher.PublishExecute gives Fatal error unhandled access violation

marcwalen
Participant Participant
1,001 Views
3 Replies
Message 1 of 4

publisher.PublishExecute gives Fatal error unhandled access violation

marcwalen
Participant
Participant

Hi all,

 

I'm having trouble with my code crashing on some, but not all,  of our workstations. When publisher.PublishExecute is executed the publish progress dialog is shown and the sheets are generated until Autocad crashes. the error messages is Unhandled Access Violation writing 0x...... Exception at ....... which is alomost always a different address. Most (but not all) of our systems with Windows 8.1 with Autocad 2015 are having the same problem. Windows 7 system with Autocad 2012 (we are slowly migrating) are having no problems. On my development machine (Windows 8.1 with Autocad 2015) it is working flawless.

 

What the code does is create a number of views and then publish it to a multipage pdf. The code for the publishing part I copied and adapted from the web.

 

here's my code:

                if (result1 == DialogResult.OK)
                {
                    //path = folderBrowserDialog1.SelectedPath;
                    path = saveFileDialog1.FileName;

                    PickListClass pickl = new PickListClass();
                    List<string> BlockErrorList = pickl.CreatePickList(path);
                    if (BlockErrorList.Count != 0)
                    {
                        BlockErrorForm bef = new BlockErrorForm();
                        bef.ShowBlockError(BlockErrorList);
                        this.Close();
                    }

                    //Editor ed = doc.Editor;

                    List<string> viewsToPlot = new List<string>();

                    panel1.Visible = true;
                    

                    // Reference the Layout Manager
                    LayoutManager acLayoutMgr = LayoutManager.Current;

                    ObjectId objID = acLayoutMgr.GetLayoutId("Tekstborden 1");

                    using (Transaction transaction = database.TransactionManager.StartTransaction())
                    {
                        // Open the layout
                        Layout acLayout = transaction.GetObject(objID, OpenMode.ForRead) as Layout;

                        int counter = 1;
                        BlockTableRecord btRecord = (BlockTableRecord)transaction.GetObject(acLayout.BlockTableRecordId, OpenMode.ForWrite);
                        //BlockTableRecord btRecord = (BlockTableRecord)transaction.GetObject(SymbolUtilityServices.GetBlockPaperSpaceId(database), OpenMode.ForRead);
                        foreach (ObjectId id in btRecord)
                        {
                            Entity ent = (Entity)transaction.GetObject(id, OpenMode.ForWrite);
                            if (ent is BlockReference)
                            {
                                BlockReference bref = (BlockReference)transaction.GetObject(id, OpenMode.ForWrite);
                                BlockTableRecord block = null;

                                if (bref.IsDynamicBlock)
                                {
                                    block = transaction.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;

                                }
                                else
                                {
                                    block = transaction.GetObject(bref.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
                                }

                                string bname = block.Name;

                                if (bname == "Form-TBoverzicht")
                                {
                                    Autodesk.AutoCAD.DatabaseServices.AttributeCollection attcol = bref.AttributeCollection;
                                    foreach (ObjectId aid in attcol)
                                    {
                                        Autodesk.AutoCAD.DatabaseServices.AttributeReference attref = (Autodesk.AutoCAD.DatabaseServices.AttributeReference)transaction.GetObject(aid, OpenMode.ForWrite);

                                        switch (attref.Tag.ToLower())
                                        {
                                            case "blad.....van":
                                                blad = attref.TextString;
                                                break;
                                            case "totaal-bladen":
                                                bladen = attref.TextString;
                                                break;
                                            case "tekeningnummer":
                                                teknr = attref.TextString;
                                                break;
                                        }
                                    }

                                    if (blad == "")
                                    {
                                        blad = counter.ToString();
                                    }
                                    if (bladen == "")
                                    {
                                        bladen = counter.ToString();
                                    }

                                    string viewName = string.Format("blad_{0}_van_{1}", blad, bladen);

                                    ViewTable vt = (ViewTable)transaction.GetObject(database.ViewTableId, OpenMode.ForRead);
                                    
                                    if (!vt.Has(viewName))
                                    {
                                        //database.UpdateExt(true);

                                        ViewTableRecord newView = CreateViewWithExtents(bref.Position, new Point3d(bref.Position.X + 210, bref.Position.Y + 297, 0.0));
                                        //newView.Bounds = new Extents3d(bref.Position, new Point3d(bref.Position.X + 210, bref.Position.Y + 297, 0.0));

                                        //newView.Layout = objID;
                                        newView.IsPaperspaceView = true;

                                        newView.ViewDirection = new Vector3d(0, 0, 1);

                                        newView.Name = viewName;

                                        vt.UpgradeOpen();

                                        vt.Add(newView);

                                        transaction.AddNewlyCreatedDBObject(newView, true);
                                    }

                                    viewsToPlot.Add(viewName);

                                }
                            }
                            counter++;
                        }


                        foreach (String viewName in viewsToPlot)
                        {
                            PlotSettings plotSettings = new PlotSettings(acLayout.ModelType);
                            plotSettings.CopyFrom(acLayout);

                            PlotSettingsValidator psv = PlotSettingsValidator.Current;

                            psv.SetPlotConfigurationName
                                (plotSettings,
                                    "bbo.pc3",
                                    "ISO_A4_(210.00_x_297.00_MM)"
                                );
                            psv.RefreshLists(plotSettings);
                            psv.SetPlotViewName(plotSettings, viewName);
                            psv.SetPlotType
                                (
                                    plotSettings,
                                    Autodesk.AutoCAD.DatabaseServices.PlotType.View
                                );
                            psv.SetUseStandardScale(plotSettings, true);
                            psv.SetStdScaleType
                                (
                                    plotSettings,
                                    StdScaleType.StdScale1To1
                                );
                            psv.SetPlotCentered(plotSettings, true);
                            psv.SetPlotRotation
                                (
                                    plotSettings,
                                    PlotRotation.Degrees000
                                );
                            psv.SetPlotPaperUnits
                                (
                                    plotSettings,
                                    PlotPaperUnit.Millimeters
                                );
                            psv.SetCurrentStyleSheet(plotSettings, "_Standaard.ctb");

                            plotSettings.PlotSettingsName = String.Format("{0}{1}", viewName, "PS");
                            plotSettings.PrintLineweights = true;
                            plotSettings.AddToPlotSettingsDictionary(database);

                            transaction.AddNewlyCreatedDBObject(plotSettings, true);
                            psv.RefreshLists(plotSettings);

                            acLayout.UpgradeOpen();
                            acLayout.CopyFrom(plotSettings);
                            acLayout.DowngradeOpen();
                        }

                        transaction.Commit();
                    }

                    
                    doc.Database.SaveAs(doc.Name, true, DwgVersion.Current, doc.Database.SecurityParameters);

                    

                    //string dwgFileName = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("DWGNAME") as string;
                    //string dwgPath = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("DWGPREFIX") as string;
                    using (Transaction Tx = database.TransactionManager.StartTransaction())
                    {
                        DsdEntryCollection collection = new DsdEntryCollection();
                        ObjectId activeLayoutId
                            = LayoutManager.Current.GetLayoutId
                                        (LayoutManager.Current.CurrentLayout);

                        foreach (String viewName in viewsToPlot)
                        {
                            Layout layout = Tx.GetObject(
                                                            activeLayoutId,
                                                            OpenMode.ForRead
                                                        ) as Layout;

                            DsdEntry entry = new DsdEntry();

                            entry.DwgName = dwgPath + dwgFileName;
                            entry.Layout = layout.LayoutName;
                            entry.Title = viewName;
                            entry.NpsSourceDwg = entry.DwgName;
                            entry.Nps = String.Format("{0}{1}", viewName, "PS");
                            
                            collection.Add(entry);
                            
                        }

                        // remove the ".dwg" extension
                        dwgFileName = dwgFileName.Substring(0, dwgFileName.Length - 4);

                        DsdData dsdData = new DsdData();
                        
                        dsdData.SheetType = SheetType.MultiPdf;
                        dsdData.ProjectPath = dwgPath;
                        //dsdData.DestinationName = path + "\\BBO_" + teknr + ".pdf";
                        dsdData.DestinationName = path;

                        if (System.IO.File.Exists(dsdData.DestinationName))
                            System.IO.File.Delete(dsdData.DestinationName);

                        dsdData.SetDsdEntryCollection(collection);

                        string dsdFile = dsdData.ProjectPath + dwgFileName + ".dsd";

                        //Workaround to avoid promp for pdf file name
                        //set PromptForDwfName=FALSE in dsdData using StreamReader/StreamWriter

                        dsdData.WriteDsd(dsdFile);

                        System.IO.StreamReader sr = new System.IO.StreamReader(dsdFile);
                        string str = sr.ReadToEnd();
                        sr.Close();

                        // Replace PromptForDwfName
                        str = str.Replace("PromptForDwfName=TRUE", "PromptForDwfName=FALSE");

                        // Workaround to have the page setup names included in the DSD file
                        // Replace Setup names based on the created page setups
                        // May not be required if Nps is output to the DSD
                        int occ = 0;
                        int index = str.IndexOf("Setup=");
                        int startIndex = 0;
                        StringBuilder dsdText = new StringBuilder();
                        while (index != -1)
                        {
                            // 6 for length of "Setup="
                            String str1 =
                            str.Substring(startIndex, index + 6 - startIndex);

                            dsdText.Append(str1);
                            dsdText.Append(
                                String.Format("{0}{1}", viewsToPlot[occ], "PS"));

                            startIndex = index + 6;
                            index = str.IndexOf("Setup=", index + 6);

                            if (index == -1)
                            {
                                dsdText.Append(
                               str.Substring(startIndex, str.Length - startIndex));
                            }
                            occ++;
                        }

                        // Write the DSD
                        System.IO.StreamWriter sw = new System.IO.StreamWriter(dsdFile);
                        sw.Write(dsdText.ToString());
                        sw.Close();

                        // Read the updated DSD file
                        dsdData.ReadDsd(dsdFile);

                        // Erase DSD as it is no longer needed
                        System.IO.File.Delete(dsdFile);

                        PlotConfig plotConfig = PlotConfigManager.SetCurrentConfig("bbo.pc3");
                        

                        Publisher publisher = Autodesk.AutoCAD.ApplicationServices.Application.Publisher;
                        
                        // Publish it
                        publisher.PublishExecute(dsdData, plotConfig);

                        Tx.Commit();
                    }
                    
                }
                Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
                this.Close();
            }

Does anyone have an idea about what is happening? I already tried reïnstalling Autocad on affaected machines following Autodesk guidelines for removing previous installation. I updated the graphics card drivers. Tried while logging in with my user profile. I can't seems to find the problem.

0 Likes
1,002 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable

You might try turning off the setting for background plotting.  Do not publish in the background.  I had a similar issue when trying to do that before in 2011.

 

A long long time ago in a firm long since forgotten I used to crash autoCAD evertime I hit the OK button from the plot dialog box.  AutoCAD would just exit no waring, nothing.  The computer didn't have enough memory to do the plot command. If that makes any sense. Maybe your Win 8 machiens need more memory to make prints?

 

Hope this helps

Matthew

0 Likes
Message 3 of 4

marcwalen
Participant
Participant

All our machines have 16 gb memory, so I don't think that's the problem. I will try turning off background plotting.

0 Likes
Message 4 of 4

marcwalen
Participant
Participant

Changing BACKGROUNDPLOT to 3 (plot and publish in background) seems to have solved it. But I think this is not a real solution, since not all machines where having the same problem.

0 Likes