.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Password protect dwf upon publishing using VF.NET

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
edweberg
806 Views, 6 Replies

Password protect dwf upon publishing using VF.NET

I recently asked about applying password protection during a SaveAs and got a great answer from Ajilal.Vijayan.  I looked in the same DevBlog for code information on how to password protect a multisheet dwf upon publishing.  Didn't find what I'm looking for. So...

 

Can someone point me in the right direction to encode password protection in multisheet dwf publish VB.NET code.  I know how to manually set password protection when publishing a dwf.  I need to do this programatically.  

 

Here's the code I'm currently using to publish multisheet dwf (paste into a Visual Studio editor; it should format correctly and be easier to read):

 

Public Sub RFS2DWF()
Try

Dim collection As New DsdEntryCollection()
Dim entry As DsdEntry

entry = New DsdEntry()
entry.Layout = "RFS21-DWF"
entry.DwgName = "I:\CAD\RFS2\RFS2-DWF\RFS21-POR.dwg"
entry.Nps = "Setup1"
entry.Title = "RFS2 Labs Level 1"
collection.Add(entry)

entry = New DsdEntry()
entry.Layout = "RFS22-DWF"
entry.DwgName = "I:\CAD\RFS2\RFS2-DWF\RFS22-POR.dwg"
entry.Nps = "Setup1"
entry.Title = "RFS2 Labs Level 2"
collection.Add(entry)

Dim dsd As New DsdData()

dsd.SetDsdEntryCollection(collection)
dsd.ProjectPath = "I:\CAD\RFS2\RFS2-DWF\"
dsd.LogFilePath = "I:\CAD\RFS2\RFS2-DWF\logdwf.log"
dsd.SheetType = SheetType.MultiDwf
dsd.NoOfCopies = 1
dsd.DestinationName = "I:\CAD\RFS2\RFS2-DWF\RFS2-POR-DWF\RFS2-POR.dwf"
dsd.SheetSetName = "PublisherSet"
dsd.WriteDsd("I:\CAD\RFS2\RFS2-DWF\publisher.dsd")

Dim nbSheets As Integer = collection.Count

Using progressDlg As New PlotProgressDialog(False, nbSheets, True)
progressDlg.UpperPlotProgressRange = 100
progressDlg.LowerPlotProgressRange = 0
progressDlg.UpperSheetProgressRange = 100
progressDlg.LowerSheetProgressRange = 0
progressDlg.IsVisible = True

Dim publisher As Autodesk.AutoCAD.Publishing.Publisher = Application.Publisher

Autodesk.AutoCAD.PlottingServices.PlotConfigManager.SetCurrentConfig("DWF6 ePlot.pc3")
publisher.PublishDsd("I:\CAD\RFS2\RFS2-DWF\publisher.dsd", progressDlg)
End Using

Catch ex As Autodesk.AutoCAD.Runtime.Exception

End Try
End Sub

 

Thank you for any useful responses.

 

Erik

6 REPLIES 6
Message 2 of 7
moogalm
in reply to: edweberg

Hi ,

 

Please use DsdData.Password API it is a read\write api.

Message 3 of 7
edweberg
in reply to: moogalm

Thank you for the reply.  I did a google search for DsdData.Password API and I don't know what to do with the information presented.  Besides City of San Diego services and a National department of statistics training pdf, I gather that ther is some 3rd party softward development group I should tap into somehow.  I'm quite at sea with all that.  

 

What I'm looking for is a way to supply a parameter or system variable or something at the time of dwf publishing from AutoCAD.  Something I can add to my publishing code which toggles the password protection and defines the password for the ensuing multi-sheet dwf.  

 

First, is that something that can be done programmatically, and second how would I go about doing that.  I suspect it is possible since I was able to programmatically apply password protection to dwg files during a SaveAs.  The manual password protection option is a checkbox in the publishing dialogue box, so I'm hoping there's a snip of code that can be added which will accomplish that.  

 

The DsdData.Password API suggestion may be very useful, but I'm not enough of an expert to know what to do with it.  That's my shortcoming.  So I'm looking for something that will send me down the right road here.  

 

Thanks,

 

Erik

Message 4 of 7
moogalm
in reply to: edweberg

 

First my apologies for getting back to you so late.

 

What I'm looking for is a way to supply a parameter or system variable or something at the time of dwf publishing from AutoCAD.  Something I can add to my publishing code which toggles the password protection and defines the password for the ensuing multi-sheet dwf.  

 

There is no API that prompts user for password however there is a work around to get it done all you need to do is edit the dsd file[ ASCII file] find for this strings "PwdProtectPublishedDWF=FALSE  and PromptForPwd=FALSE" turn them true if they are false.

 

Here is full sample , and test sample is available at publishing-model-views-to-a-multi-sheet-dwf

 

[CommandMethod("PublishViews2MultiSheet")]
        static public void PublishViews2MultiSheet()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            StringCollection viewsToPlot = new StringCollection();
            viewsToPlot.Add("Test1");
            viewsToPlot.Add("Test2");
            using (Transaction Tx = db.TransactionManager.StartTransaction())
            {
                ObjectId layoutId = LayoutManager.Current.GetLayoutId(LayoutManager.Current.CurrentLayout);
                Layout layout = Tx.GetObject(layoutId, OpenMode.ForWrite) as Layout;
                foreach (String viewName in viewsToPlot)
                {
                    PlotSettings plotSettings = new PlotSettings(layout.ModelType);
                    plotSettings.CopyFrom(layout);
                    PlotSettingsValidator psv = PlotSettingsValidator.Current;
                    psv.SetPlotConfigurationName(plotSettings, "DWF6 ePlot.pc3", "ANSI_A_(8.50_x_11.00_Inches)");
                    psv.RefreshLists(plotSettings);
                    psv.SetPlotViewName(plotSettings, viewName);
                    psv.SetPlotType(plotSettings, Autodesk.AutoCAD.DatabaseServices.PlotType.View);
                    psv.SetUseStandardScale(plotSettings, true);
                    psv.SetStdScaleType(plotSettings, StdScaleType.ScaleToFit);
                    psv.SetPlotCentered(plotSettings, true);
                    psv.SetPlotRotation(plotSettings, PlotRotation.Degrees000);
                    psv.SetPlotPaperUnits(plotSettings, PlotPaperUnit.Inches);
                    plotSettings.PlotSettingsName = String.Format("{0}{1}", viewName, "PS");
                    plotSettings.PrintLineweights = true;
                    plotSettings.AddToPlotSettingsDictionary(db);
                    Tx.AddNewlyCreatedDBObject(plotSettings, true);
                    psv.RefreshLists(plotSettings);
                    layout.CopyFrom(plotSettings);
                }
                Tx.Commit();
            }
            short bgPlot = (short)Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("BACKGROUNDPLOT");
            Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("BACKGROUNDPLOT", 0);
            string dwgFileName = Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("DWGNAME") as string;
            string dwgPath = Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("DWGPREFIX") as string;
            using (Transaction Tx = db.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);
                }
                dwgFileName = dwgFileName.Substring(0, dwgFileName.Length - 4);
                DsdData dsdData = new DsdData();
                dsdData.SheetType = SheetType.MultiDwf;
                dsdData.ProjectPath = dwgPath;
                dsdData.DestinationName = dsdData.ProjectPath + dwgFileName + ".dwf";
                if (System.IO.File.Exists(dsdData.DestinationName)) System.IO.File.Delete(dsdData.DestinationName);
                dsdData.SetDsdEntryCollection(collection);
                string dsdFile = dsdData.ProjectPath + dwgFileName + ".dsd";
                dsdData.WriteDsd(dsdFile);
                System.IO.StreamReader sr = new System.IO.StreamReader(dsdFile);
                string str = sr.ReadToEnd();
                sr.Close();
                str = str.Replace("PromptForDwfName=TRUE", "PromptForDwfName=FALSE");
                /*Prompts User to Enter Password and Reconfirms*/
                str = str.Replace("PromptForPwd=FALSE", "PromptForPwd=TRUE");
                str = str.Replace("PwdProtectPublishedDWF=FALSE", "PwdProtectPublishedDWF=TRUE");
                int occ = 0;
                int index = str.IndexOf("Setup=");
                int startIndex = 0;
                StringBuilder dsdText = new StringBuilder();
                while (index != -1)
                {
                    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++;
                }
                System.IO.StreamWriter sw = new System.IO.StreamWriter(dsdFile);
                sw.Write(dsdText.ToString());
                sw.Close();
                dsdData.ReadDsd(dsdFile);
                System.IO.File.Delete(dsdFile);
                PlotConfig plotConfig = PlotConfigManager.SetCurrentConfig("DWF6 ePlot.pc3");
                Publisher publisher = Autodesk.AutoCAD.ApplicationServices.Core.Application.Publisher;
                publisher.PublishExecute(dsdData, plotConfig);
                Tx.Commit();
            }
            Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
        }

 Hope this solves your problem.

Message 5 of 7
edweberg
in reply to: moogalm

I was able to use portions of the code you supplied to edit the dsd file so that PromptForPws and PwsProtectPublishedDWF are each true.  However, when running the publish application, it neither propmpted for password nor password protected the created dwf file.  I opened the dsd file as a text file, and both those parameters are "True", but nothing acutally happened.

 

I did some googling to see if I could figure out how to implement what you sent, but still not able to use it and make it work.  

 

Upthread I've included a piece of code which works to publish a multi-sheet dwf, the sheets being layouts from two different drawings.  Is there any way you could show me how to use code to set password protection at the time of publishing, using a password provided in the code?  

 

I do appreciate all the help.  I'm not an expert at this stuff; in large part, I assemble code snippets I can gather from the web and other users on this forum and others.  

 

To summarize what I'm trying to do:

- I have automation whereby I push a button and a multi-sheet dwf gets published; sheets are layouts from different drawings (code is above).

- I need to have the automation work the exact same way, but the published dwf needs to be password protected and the password supplied in the code, not something a user has to enter at time of publishing.

 

Thanks,

 

Erik

 

Message 6 of 7
moogalm
in reply to: edweberg

Firstly, I'm not sure why it is not working at you around, anyway if you want to hardcode password with in the code you can something like this. please put dsdData.Password = "abcdefg", it is C# code you can easliy translate to VB.NET

 

Something like this.

 

static public void PublishViews2MultiSheet()
        {
            //pwdWindow = new PasswordWindow();
            //pwdWindow.ShowDialog();
            Document doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            StringCollection viewsToPlot = new StringCollection();
            viewsToPlot.Add("Test1");
            viewsToPlot.Add("Test2");
            using (Transaction Tx = db.TransactionManager.StartTransaction())
            {
                ObjectId layoutId = LayoutManager.Current.GetLayoutId(LayoutManager.Current.CurrentLayout);
                Layout layout = Tx.GetObject(layoutId, OpenMode.ForWrite) as Layout;
                foreach (String viewName in viewsToPlot)
                {
                    PlotSettings plotSettings = new PlotSettings(layout.ModelType);
                    plotSettings.CopyFrom(layout);
                    PlotSettingsValidator psv = PlotSettingsValidator.Current;
                    psv.SetPlotConfigurationName(plotSettings, "DWF6 ePlot.pc3", "ANSI_A_(8.50_x_11.00_Inches)");
                    psv.RefreshLists(plotSettings);
                    psv.SetPlotViewName(plotSettings, viewName);
                    psv.SetPlotType(plotSettings, Autodesk.AutoCAD.DatabaseServices.PlotType.View);
                    psv.SetUseStandardScale(plotSettings, true);
                    psv.SetStdScaleType(plotSettings, StdScaleType.ScaleToFit);
                    psv.SetPlotCentered(plotSettings, true);
                    psv.SetPlotRotation(plotSettings, PlotRotation.Degrees000);
                    psv.SetPlotPaperUnits(plotSettings, PlotPaperUnit.Inches);
                    plotSettings.PlotSettingsName = String.Format("{0}{1}", viewName, "PS");
                    plotSettings.PrintLineweights = true;
                    plotSettings.AddToPlotSettingsDictionary(db);
                    Tx.AddNewlyCreatedDBObject(plotSettings, true);
                    psv.RefreshLists(plotSettings);
                    layout.CopyFrom(plotSettings);
                }
                Tx.Commit();
            }
            short bgPlot = (short)Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("BACKGROUNDPLOT");
            Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("BACKGROUNDPLOT", 0);
            string dwgFileName = Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("DWGNAME") as string;
            string dwgPath = Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("DWGPREFIX") as string;
            using (Transaction Tx = db.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);
                }
                dwgFileName = dwgFileName.Substring(0, dwgFileName.Length - 4);
                DsdData dsdData = new DsdData();
                dsdData.SheetType = SheetType.MultiDwf;
                dsdData.ProjectPath = dwgPath;
                dsdData.DestinationName = dsdData.ProjectPath + dwgFileName + ".dwf";
                /*Hardcode password*/
                dsdData.Password=  "123456";
                if (System.IO.File.Exists(dsdData.DestinationName)) System.IO.File.Delete(dsdData.DestinationName);
                dsdData.SetDsdEntryCollection(collection);

                /*DsdFile */
                string dsdFile = dsdData.ProjectPath + dwgFileName + ".dsd";
                dsdData.WriteDsd(dsdFile);
                System.IO.StreamReader sr = new System.IO.StreamReader(dsdFile);
                string str = sr.ReadToEnd();
                sr.Close();
                str = str.Replace("PromptForDwfName=TRUE",
                                   "PromptForDwfName=FALSE");
                str = str.Replace("PwdProtectPublishedDWF=FALSE", 
                                  "PwdProtectPublishedDWF=TRUE");
                int occ = 0;
                int index = str.IndexOf("Setup=");
                int startIndex = 0;
                StringBuilder dsdText = new StringBuilder();
                while (index != -1)
                {
                    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++;
                }
                System.IO.StreamWriter sw = new System.IO.StreamWriter(dsdFile);
                sw.Write(dsdText.ToString());
                sw.Close();
                dsdData.ReadDsd(dsdFile);
                System.IO.File.Delete(dsdFile);
                PlotConfig plotConfig = PlotConfigManager.SetCurrentConfig("DWF6 ePlot.pc3");
                Publisher publisher = Autodesk.AutoCAD.ApplicationServices.Core.Application.Publisher;
                publisher.PublishExecute(dsdData, plotConfig);
                Tx.Commit();
            }
            Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
        }

 

Message 7 of 7
edweberg
in reply to: moogalm

Fantastic!  I was able to pick the bits I need and apply them to my code.  Works like a charm.  Thank you for the help and direction.

 

Erik

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost