Author Topic: Wblock and saveas version  (Read 7682 times)

0 Members and 1 Guest are viewing this topic.

Bryco

  • Water Moccasin
  • Posts: 1883
Wblock and saveas version
« on: September 10, 2009, 11:47:24 AM »
I'm trying to force a wblock to saveas to 2004 as we have some software (cnc) that will not read a newer version.
The doc_CommandEnded event allows me to access the wblock end but gives no useful info as the database and doc are still those of the begin command.
Any ideas? (There is no commandline text showing the path of the wblock)

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Wblock and saveas version
« Reply #1 on: September 10, 2009, 04:54:02 PM »
Code: [Select]
static void db_WblockNotice(object sender, WblockNoticeEventArgs e)
        {
            Database wblockDb = e.To;
            doc.Editor.WriteMessage(wblockDb.Filename);
            doc.Editor.WriteMessage(wblockDb.OriginalFileName);
        }

The help for .To = Gets the database being written to.
however it prints the original dwg name.  So not getting any closer.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Wblock and saveas version
« Reply #2 on: September 14, 2009, 05:40:53 PM »
Just found out the above fires for copyclip.

Chumplybum

  • Newt
  • Posts: 97
Re: Wblock and saveas version
« Reply #3 on: September 14, 2009, 07:43:38 PM »
are you wblocking from the wblock command or via code???


Bryco

  • Water Moccasin
  • Posts: 1883
Re: Wblock and saveas version
« Reply #4 on: September 14, 2009, 07:48:44 PM »
I'm trying to the event db_WblockNotice to trap any wblock command with the intention of saving the drawing to an older version.
So the answer is wblock command.

Chumplybum

  • Newt
  • Posts: 97
Re: Wblock and saveas version
« Reply #5 on: September 14, 2009, 08:22:14 PM »
give this a go, it'll return the path to the newly created drawing file (it grabs the info from the registry, instead of the database)

Code: [Select]
        Private Sub db_WblockNotice(ByVal sender As Object, ByVal e As WblockNoticeEventArgs)

            Dim regkey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(Autodesk.AutoCAD.Runtime.SystemObjects.DynamicLinker.ProductKey)
            Dim wblockreg As Microsoft.Win32.RegistryKey = regkey.OpenSubKey("Profiles\Mark\Dialogs\write_block_dialog")

            MsgBox(wblockreg.GetValue("MRUPathListItem0"))

        End Sub

EDIT: sorry Bryco, i just notice that you'll need to put the name of the current profile in instead of my name under the opensubkey function

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Wblock and saveas version
« Reply #6 on: September 15, 2009, 05:02:11 PM »
That is exactly what I want Mark, thanks.

Still crashing trying to save it, however
Code: [Select]
       static void db_WblockNotice(object sender, WblockNoticeEventArgs e)
        {      
            Microsoft.Win32.RegistryKey regkey=Microsoft.Win32.Registry.CurrentUser.OpenSubKey
                (Autodesk.AutoCAD.Runtime.SystemObjects.DynamicLinker.ProductKey);
            Microsoft.Win32.RegistryKey  wblockreg=regkey.OpenSubKey
                (@"Profiles\" +acadApp.GetSystemVariable("Cprofile").ToString()+@"\Dialogs\write_block_dialog");        
            string sFile=wblockreg.GetValue("MRUPathListItem0").ToString();
           // MessageBox.Show(wblockreg.GetValue("MRUPathListItem0").ToString());
            Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
            using(Database wblockDatabase = new Database(false, true))
            {
                try
                {
                    wblockDatabase.ReadDwgFile(sFile, System.IO.FileShare.ReadWrite, true, null);

                    if (wblockDatabase != HostApplicationServices.WorkingDatabase)  
                        HostApplicationServices.WorkingDatabase = wblockDatabase;
                    wblockDatabase.SaveAs(sFile, DwgVersion.AC1800a);   //2004 final
                    //wblockDatabase.SaveAs(sFile, DwgVersion.AC1500);
                }
                catch (Autodesk.AutoCAD.Runtime.Exception acEx)
                {
                    ed.WriteMessage("{0}AutoCAD Runtime Exception: {1}", Environment.NewLine, acEx.Message);
                    return;
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage("{0}System Runtime Exception: {1}", Environment.NewLine, ex.ToString());
                    return;
                }
                finally
                {
                }

            }          
        }

The error is eInvaliddrawingversion , however when I use "wblockDatabase.SaveAs(sFile, DwgVersion.Current);" there is no error reported and it crashes
« Last Edit: September 15, 2009, 05:06:35 PM by Bryco »

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Wblock and saveas version
« Reply #7 on: September 15, 2009, 06:07:40 PM »
These 2 lines cause the crash
if (wblockDatabase != HostApplicationServices.WorkingDatabase) 
           HostApplicationServices.WorkingDatabase = wblockDatabase;
but after commenting them out I still get the error.

I only just noticed that if you don't use the file selection box when  wblocking the new "wrote" dwg will be saved in the version that you have chosen in your saveas options, as soon as you browse it saves it to the same version of cad you are using. Bloody confusing when you are testing the code.

Chumplybum

  • Newt
  • Posts: 97
Re: Wblock and saveas version
« Reply #8 on: September 15, 2009, 07:00:32 PM »
i can see a few problems, unfortunately i don't have a solution at this stage....

there's no need to set the working database to the wblockdatabase, once you have a database object you can use the save/saveas function without setting it to the working database.... unless you've got another reason to set it to the working database?

the wblocknotice event gets fired during the 'wblocking', which at this point the new dwg file has not been created. put a stop on the readdwgfile line and then browse to the location of the newly created drawing and you'll see that its not there yet.


i've been trying to use the wblockended event but haven't been able to get the sub to run, here is the code so far:
Code: [Select]
        <CommandMethod("Test")> _
        Public Sub Test()

            AddHandler Application.DocumentManager.MdiActiveDocument.Database.WblockEnded, AddressOf db_WblockEnded

        End Sub

        Public Sub db_WblockEnded(ByVal sender As Object, ByVal e As System.EventArgs)

            Dim regkey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(Autodesk.AutoCAD.Runtime.SystemObjects.DynamicLinker.ProductKey)
            Dim wblockreg As Microsoft.Win32.RegistryKey = regkey.OpenSubKey("Profiles\Mark\Dialogs\write_block_dialog")
            Dim filename As String = wblockreg.GetValue("MRUPathListItem0")

            Using wblockdb As Database = New Database(False, True)
                Try
                    wblockdb.ReadDwgFile(filename, IO.FileShare.ReadWrite, False, Nothing)

                    If wblockdb.OriginalFileVersion <> DwgVersion.AC1800a Then wblockdb.SaveAs(filename, DwgVersion.AC1800a)

                Catch ex As Exception

                End Try
            End Using

        End Sub

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Wblock and saveas version
« Reply #9 on: September 15, 2009, 07:05:59 PM »
I couldn't get wblockended to do anything.
However I've been adding  the WblockNotice event  "doc.Database.WblockNotice +=new WblockNoticeEventHandler(db_WblockNotice);" from the begincommand which fires after the browsing has been done so timing wise seems ok.

Chumplybum

  • Newt
  • Posts: 97
Re: Wblock and saveas version
« Reply #10 on: September 15, 2009, 07:14:09 PM »
this seems to work now (using the command ended event), i have a feeling the problem is with the DwgVersion.AC1800a which i've changed to AC1800 and it seems to work

Code: [Select]
    Public Class Class1
        <CommandMethod("Test")> _
        Public Sub Test()

            AddHandler Application.DocumentManager.MdiActiveDocument.CommandEnded, AddressOf db_CommandEnded

        End Sub

        Public Sub db_CommandEnded(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.ApplicationServices.CommandEventArgs)

            If String.Compare(e.GlobalCommandName, "WBLOCK", False) = 0 Then
                Dim regkey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(Autodesk.AutoCAD.Runtime.SystemObjects.DynamicLinker.ProductKey)
                Dim wblockreg As Microsoft.Win32.RegistryKey = regkey.OpenSubKey("Profiles\Mark\Dialogs\write_block_dialog")
                Dim filename As String = wblockreg.GetValue("MRUPathListItem0")

                Using wblockdb As Database = New Database(False, True)
                    Try
                        wblockdb.ReadDwgFile(filename, IO.FileShare.ReadWrite, False, Nothing)

                        If wblockdb.OriginalFileVersion <> DwgVersion.AC1800 Then wblockdb.SaveAs(filename, DwgVersion.AC1800)

                    Catch ex As Exception

                    End Try
                End Using
            End If

        End Sub
    End Class

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Wblock and saveas version
« Reply #11 on: September 15, 2009, 08:02:40 PM »
well done chumplybum. I'm not quite sure why this worked and the other didn't though.
I have just noticed that when you hit the browse button in the "Write Block" gui, the browse for drawing gui has a "Files of type" option at the bottom.
I could conceivably set that option instead, by code. But I've spent a long time on this already so another time.

Thanks again Mark

Glenn R

  • Guest
Re: Wblock and saveas version
« Reply #12 on: September 17, 2009, 04:09:49 PM »
Bryco,

I have some ideas, but I won't get to this until this weekend. Having said that, I would like some more information.

1. What ACAD version
2. What OS and SP
3. What Framework version and SP if any.
4. What Vis Stud version and SP if any.

If you have a look in the ARX group here, I asked if anybody could supply the 2010 version of ARXDBG (I haven't touched ARX in 5 years or so and as a consequence I'm very rusty) and Owen was kind enough to supply it.

This tool is extremely handy in diagnosing problems (along with DBVIEW), especially reactors.

From my brief look tonight, I have some suspicions. I'll explain more on the weekend once I've done some more testing. It might not come to anything, but I have some theories to try.

Cheers,
Glenn.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Wblock and saveas version
« Reply #13 on: September 17, 2009, 04:23:28 PM »
1) acad2010  using vanilla from acad mechanical
2)  xp pro sp 3
3)Microsoft Visual C# 2008 ver 3.5 sp1

I did play w/ below
Code: [Select]
//Microsoft.Win32.RegistryKey wblockreg = regkey.OpenSubKey
                                //(@"Profiles\" + acadApp.GetSystemVariable("Cprofile").ToString() + @"\Dialogs\BrowseforDrawingFileBlkDlg");
                                //This key set to 2 sets the dialogue box to saveas 2004  but cant get it to work
Perhaps if I did this once at every startup I wouldn't have to do it again. As I said above it's the file selection box that causes the problem.

Glenn R

  • Guest
Re: Wblock and saveas version
« Reply #14 on: September 17, 2009, 05:00:56 PM »
What I have in mind won't give a rats about the dialog selection, which is not a good way to go IMHO.

More on the weekend when I have time.

Have a good one.

Cheers,
Glenn.