Through the Interface: Exporting to DWG from the Clipboard Manager

May 2015

Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30


« November’s Plugin of the Month live on Autodesk Labs: ScriptPro 2.0 for AutoCAD | Main | Two minor fixes to Clipboard Manager and much data-bound frustration »

November 01, 2010

Exporting to DWG from the Clipboard Manager

I’ve made a couple of updates to the Clipboard Manager in this latest version.

Firstly I wanted to remove a peculiar situation that occurred in the previous versions when you copy something to the clipboard that isn’t AutoCAD geometry: for some reason this act invalidates the ability to access data – such as the preview images – from the items in the Clipboard Manager. The pasting into the drawing still works, as setting an AutoCAD object back onto the clipboard re-enables access to AutoCAD data. You can try this with the previous version by creating a number of entries on the Clipboard Manager before copying some text to the clipboard from Notepad (although the issue was first reported with copying text from the MText IPE). Doing so stops the preview from working properly (and whether that blanks the preview image or causes an old one to remain depends on the version you’re using).

Anyway, the fix I decided upon is simple enough but could create user confusion: if the preview image is blank – as we attempt to retrieve it – when selection is changed in the Clipboard Manager, then we copy the contents of that entry to the clipboard. This allows us to get the preview image, and life is good. The downside is that this clears the previous contents of the clipboard – which may well be undesirable from the user’s perspective. That said, the fact that the user has clicked onto the Clipboard Manager palette does imply they want to use it for something, and so perhaps this is an acceptable compromise. Let me know if you have thoughts on this – I’d be happy to hear them.

The second change is the promised “Export to DWG” capability. I thought this would be very easy to implement – as we already had our function to get the temporary DWG file from AutoCAD’s clipboard data – but it proved harder than expected for one reason: AutoCAD only keeps the temporary DWG while the contents are on the clipboard. As soon as an item gets relegated to 2nd place, its temporary DWG file gets deleted from disk! This took me a while to work out and frankly I’m still scratching my head as to how the Clipboard Manager actually functions at all: if the DWG is inaccessible then PASTECLIP must retrieve it or regenerate it from somewhere else, but I don’t know how or why. At some point I may dig a little deeper into the implementation of the PASTECLIP command, but for now I’ll just be happy that it works when prior clipboard objects are copied back onto the clipboard.

The logical way to get around this situation is to copy the DWG file while it’s still valid, when the entry is first placed onto the clipboard. This means we then have to maintain a list of these temporary DWGs that might get used by the Export option but then they might very well not: this seems an acceptable overhead, especially if we clean up after ourselves. We then also have to store the location of this DWG file along with the DataObject defining the clipboard contents. We were previously just storing the DataObject in the “tag” property of each DataGridViewRow (a very handy field that can be used to associate arbitrary data with an item in the list). Now that we have more data to store we’ll instead store a key into a dictionary of “clipboard data items”, each of which stores the DataObject and the temporary DWG path. You’ll find this in the new “Clipboard_Data.vb” file implementing the CbDataManager class which manages items of class CbEntry. This manager object also knows how to clean up after itself by deleting temporary DWGs from disk when items are removed from the list or when the session finishes.

The resultant EXPORTCB command (as wherever possible we want to drive commands from modeless dialogs such as palettes) ends up being pretty simple:

  <CommandMethod("ADNPLUGINS", "EXPORTCB", CommandFlags.Modal)> _

  Public Shared Sub ExportClipboard()


    ' Make sure we only have one item selected


    If _cp.clipboardDataGridView.SelectedRows.Count = 1 Then


      ' Get the selected item and its associated DWG file


      Dim item As DataGridViewRow = _


      Dim dwgFile As String = _cp.GetTempDwg(item.Tag)

      If Not String.IsNullOrEmpty(dwgFile) Then


        Dim ed As Editor = _

          Autodesk.AutoCAD.ApplicationServices.Application _



        ' Get the name of the entry as shown in the list


        Dim itemName As String = item.Cells(0).FormattedValue


        ' Set up our Save dialog options


        Dim opts As PromptSaveFileOptions = _

          New PromptSaveFileOptions( _

          "Export """ + itemName + """ Contents To")

        opts.Filter = "Drawing (*.dwg)|*.dwg"

        opts.InitialDirectory = _


        opts.InitialFileName = itemName + ".dwg"


        ' Ask the user to selected the output location


        Dim pr As PromptFileNameResult = ed.GetFileNameForSave(opts)

        If pr.Status = PromptStatus.OK Then


            Dim dwgDest As String = pr.StringResult


            ' If the file exists, the user has already

            ' chosen to overwrite it


            If File.Exists(dwgDest) Then


            End If


            ' Copy the file


            File.Copy(dwgFile, dwgDest)


            ' Inform the user of success


            ed.WriteMessage( _

              vbCr + "Exported contents of ""{0}"" to ""{1}""", _

              itemName, dwgDest)


          End Try

        End If

      End If

    End If


  End Sub

Here’s the Clipboard Manager containing four nicely-renamed entries:

Clipboard Manager containing four entries

Here’s the temporary location containing our four DWG files:

Our corresponding (temporary) DWG filesAs items get removed from the list, the corresponding DWG files also get removed. When we want to export a particular item to DWG, we simply right-click it and select “Export to DWG”:

Exporting to a DWG from the Clipboard ManagerAt which point we see our file selection dialog for the destination DWG file:

Our file selection dialog for the destination DWGAnd selecting “Save” will result in a simple file-copy from the temporary file to our specified destination, and a confirmation on the command-line:

Exported contents of "Six Circles" to "C:\Users\walmslk\Documents\Six Circles.dwg"

That’s it for today’s update. There’s one last issue that’s been reported that I’m working through: sometimes you need to click twice in the drawing after selecting “paste” from a newly-selected item for it to place the geometry. There’s clearly some kind of focus-related issue, but it’s proving a little tricky to chase down (such issues often prove tough to debug). Hopefully I’ll find a way to address it, otherwise I’ll just recommend people to right-click directly, which appears to avoid the problem.

blog comments powered by Disqus


10 Random Posts