Author Topic: Open Issues  (Read 1552 times)

0 Members and 1 Guest are viewing this topic.

flowerrobot

  • Guest
Open Issues
« on: September 15, 2011, 05:53:07 PM »
Gday Guys

I have been suffering two issues on my final implentation of a program and would really love some input.

1. Where do you define a class so that all documents can access it
2. Using the DocumentCollection.open class the file is opened but not made active.


Let me elaborate, I have a class that will manage and remember a lot of items, files & directorys. The reason for this is because there is large time saving issues. However the way I am defining it now, a new class is created when the command is run for the first time for each document, I would like all documents to access the identical class, So that in the entire session on acad there is only 1 class ever.
All runs fine on the first document till the end where I open a file. It opens file but it is not made the active document, which is easily sorted. However upon the last document been opened and made active it causes issues when the command is then run in this new document, As in it freezes autocad2012 & VS2010.


Thanks alot for your help!


Code: [Select]
Namespace OpenFiles_AutoCad

    ' 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 MyCommands
        Dim NuffFiler As OpenFileForm    '<--------------- this is the class I wont one instance of per autocad
        ' CommandFlags.Modal
        <CommandMethod("Nuff")> _
        Public Sub NuffCommand() ' This method can have any name
            Dim AcDocCol As DocumentCollection = Application.DocumentManager

            'Get data from registry or last opened project info & get data from current document if document is a related file.
            Dim RegData() As String = GetRegHistory()
            Dim FileData() As String = GetCurrentAutocadFileData()

            'If open document is revalent make it the active information
            If FileData IsNot Nothing Then
                FileData.CopyTo(RegData, 0)
            End If

            'Check if class has been created, If not create, Eles reset settings
            If NuffFiler Is Nothing Then
                NuffFiler = New OpenFileForm(RegData)
            Else
                NuffFiler.Reset(RegData)
            End If

            Try
                'Get files to open
                Application.ShowModalDialog(NuffFiler)

                If NuffFiler.HasFilesToOpen Then
                    'Because documents are not be made active remember last file
                    Dim LastDoc As Document = Nothing
                    'Open all files in list
                    For Each FileName As String In NuffFiler.FilesToOpen
                        Try
                            LastDoc = AcDocCol.Open(FileName, False)
                        Catch
                        End Try
                    Next
                    'Check if files have been open is so make it active
                    'If LastDoc IsNot Nothing Then
                    '    Try
                    '        AcDocCol.MdiActiveDocument = LastDoc
                    '    Catch
                    '    End Try
                    'End If
                    'To Do get information & put into history

                End If
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
            End Try
        End Sub



Jeff H

  • Needs a day job
  • Posts: 6151
Re: Open Issues
« Reply #1 on: September 15, 2011, 06:44:09 PM »
For each document created where any of its method's are called that is marked with a   <CommandMethod>  attribute
AutoCAD will create a instance of that class, so each document gets it's own instance.

I am not sure about your application but you try this,
 
If you declare a member of a class as Shared then it will be available to every instance of that class
 
 
So just change Dim to Shared
 
 Shared NuffFiler As OpenFileForm

flowerrobot

  • Guest
Re: Open Issues
« Reply #2 on: September 15, 2011, 08:26:45 PM »
Champion, That did it thanks, I've not used the 'shared ' before. Thanks for the insite.

Any idears about the opening?

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Open Issues
« Reply #3 on: September 15, 2011, 09:55:01 PM »
For making a document active you set
DocumentCollection.MdiActiveDocument to the new document.
 
But now  you are wanting to step back and work across multiple documents so you need to be in 'Application Context' using CommandFlag Session
<CommandMethod("Nuff", CommandFlags.Session)>