• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    udo
    Active Member
    Posts: 6
    Registered: ‎05-09-2012
    Accepted Solution

    Save a password protected drawing without password with VB.NET

    276 Views, 3 Replies
    05-09-2012 06:57 AM

    Hello,

     

    our drawings are protected by a password.

    We have a VB.NET programm to export a drawing without password.

     

    ...

    Dim securityPar As New SecurityParameters(Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)

    doc.Database.SaveAs("C:\Temp\Test.DWG", False, DwgVersion.Current, securityPar)

    doc.CloseAndSave("C:\Temp\Test.DWG")

    ...

     

    In AutoCAD Architecture 2009 (Win XP) it works fine.

    In AutoCAD Architecture 2011 (Win 7) the drawing is still password protected.

     

    Thank you for help,

     

    Udo

    Please use plain text.
    Moderator
    Alexander.Rivilis
    Posts: 1,167
    Registered: ‎04-09-2008

    Re: Save a password protected drawing without password with VB.NET

    05-09-2012 08:27 AM in reply to: udo

    With help of ObjecARX (native) you can clear password protection: http://forums.autodesk.com/t5/Autodesk-ObjectARX/any-way-to-remove-password-from-a-dwg/m-p/3429797#M...

    I've not found corresponding method AutoCAD .NET API. IMHO you have to P/Invoke setSecurityParams method.


    Пожалуйста не забывайте про Утвердить в качестве решения!Утвердить в качестве решения и Give Kudos!Баллы
    Please remember to Accept Solution!Accept as Solution and Give Kudos!Kudos

    Please use plain text.
    Moderator
    Alexander.Rivilis
    Posts: 1,167
    Registered: ‎04-09-2008

    Re: Save a password protected drawing without password with VB.NET

    05-09-2012 02:27 PM in reply to: Alexander.Rivilis

    Try this code (minimum testing with AutoCAD 2012 x86):

     

    using System;
    using System.Runtime.InteropServices;
    using Autodesk.AutoCAD.Runtime;
    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.Geometry;
    using Autodesk.AutoCAD.EditorInput;
    
    // This line is not mandatory, but improves loading performances
    [assembly: CommandClass(typeof(Rivilis.ClearPassword))]
    
    namespace Rivilis
    {
    
        // This class is instantiated by AutoCAD for each document when
        // a command is called by the user the first time in the context
        // of a given document. In other words, non static data in this class
        // is implicitly per-document!
        public class ClearPassword
        {
            [StructLayout(LayoutKind.Sequential)]
            // From dbsecurity.h
            public struct SecurityParams
            {
                public uint cbSize;
                public uint ulFlags;    // see enum above for flag values
                // data relevant to password protection
                public string wszPassword;
                public uint ulProvType;
                public string wszProvName;
                public uint ulAlgId;        // SECURITYPARAMS_ALGID_RC4
                public uint ulKeyLength;
                // data relevant to digital signatures
                public string wszCertSubject;
                public string wszCertIssuer;
                public string wszCertSerialNum;
                public string wszComment;
                public string wszTimeServer;
            };
            [DllImport("acdb18.dll", CallingConvention = CallingConvention.ThisCall,
                EntryPoint = "?setSecurityParams@AcDbDatabase@@QAE_NPBUSecurityParams@@_N@Z")]
            static extern private bool setSecurityParamsX32(IntPtr db, ref SecurityParams pSecParams, bool bSetDbMod = true);
            [DllImport("acdb18.dll", CallingConvention = CallingConvention.ThisCall, 
                EntryPoint = "?setSecurityParams@AcDbDatabase@@QEAA_NPEBUSecurityParams@@_N@Z")]
            static extern private bool setSecurityParamsX64(IntPtr db, ref SecurityParams pSecParams, bool bSetDbMod = true);
    
            static public bool setSecurityParams(Database db, ref SecurityParams parms, bool bSetDbMod = true)
            {
                if (IntPtr.Size == 4) return setSecurityParamsX32(db.UnmanagedObject, ref parms, bSetDbMod);
                         else         return setSecurityParamsX64(db.UnmanagedObject, ref parms, bSetDbMod);
            }
    
            [CommandMethod("ClearPWD", CommandFlags.Modal)]
            public void ClearPWD()
            {
                Database db = HostApplicationServices.WorkingDatabase;
                SecurityParams parms = new SecurityParams();
                parms.cbSize = (uint)Marshal.SizeOf(parms);
                parms.ulFlags = 0; // Clear security flag
                setSecurityParams(db, ref parms);
            }
        }
    }
    

    For VB.NET you can convert C# code: http://www.developerfusion.com/tools/convert/csharp-to-vb/

     


    Пожалуйста не забывайте про Утвердить в качестве решения!Утвердить в качестве решения и Give Kudos!Баллы
    Please remember to Accept Solution!Accept as Solution and Give Kudos!Kudos

    Please use plain text.
    udo
    Active Member
    Posts: 6
    Registered: ‎05-09-2012

    Re: Save a password protected drawing without password with VB.NET

    05-24-2012 12:24 AM in reply to: Alexander.Rivilis

    Hello Alexander,

     

    thank you! It works very fine in AutoCAD Architecture 2011.

     

    Udo

    Please use plain text.