locked
How to Export diagram as image RRS feed

  • Question

  • Hi,
    Is it (or will it be) possible to export a diagram created with a DSL as an image just like the Visual Studio Class Designer does?
    If not, how could I implement it?
    Thanks!
    Mike
    Wednesday, June 15, 2005 8:02 PM

Answers

  • First, I must give the caveat that we're working through our API design at the moment, so all of this is subject to change, it's only a CTP at this point, etc.

    That said, the core functionality used by the Class Designer is currently available via the Microsoft.VisualStudio.Modeling.Diagrams.Diagram.CreateBitmap() API.  Given an instance of your diagram, you can make this call, passing in a collection of shapes (or the collection Diagram.NestedChildShapes if you want to include all shapes on the diagram), and it will return a Bitmap object which you can choose to save to a file, place on the clipboard, etc.

    If you want something even closer to the Class Designer experience, there is a class called Microsoft.VisualStudio.EnterpriseTools.Shell.DiagramExporter, in the assembly Microsoft.VisualStudio.Modeling.Sdk.Shell.dll (already referenced from your Designer project if you created a solution via the DSL designer wizard).  You can construct an instance of this class given an object that implements IServiceProvider, and then it is just a matter of calling DiagramExporter.ExportDiagram (or ExportDiagrams to do multiple diagrams at once).  This takes a path (paths) to your diagram file(s), the directory on disk you where you want the diagrams exported, the image format you want to use, and a boolean value indicating whether existing diagrams should be overwritten.

    I know I've left some things a little vague in this explanation, if you try to use these classes and you can't figure something out, let me know.

    Thanks,
    Grayson Myers [MSFT]

    Wednesday, June 15, 2005 11:51 PM

All replies

  • First, I must give the caveat that we're working through our API design at the moment, so all of this is subject to change, it's only a CTP at this point, etc.

    That said, the core functionality used by the Class Designer is currently available via the Microsoft.VisualStudio.Modeling.Diagrams.Diagram.CreateBitmap() API.  Given an instance of your diagram, you can make this call, passing in a collection of shapes (or the collection Diagram.NestedChildShapes if you want to include all shapes on the diagram), and it will return a Bitmap object which you can choose to save to a file, place on the clipboard, etc.

    If you want something even closer to the Class Designer experience, there is a class called Microsoft.VisualStudio.EnterpriseTools.Shell.DiagramExporter, in the assembly Microsoft.VisualStudio.Modeling.Sdk.Shell.dll (already referenced from your Designer project if you created a solution via the DSL designer wizard).  You can construct an instance of this class given an object that implements IServiceProvider, and then it is just a matter of calling DiagramExporter.ExportDiagram (or ExportDiagrams to do multiple diagrams at once).  This takes a path (paths) to your diagram file(s), the directory on disk you where you want the diagrams exported, the image format you want to use, and a boolean value indicating whether existing diagrams should be overwritten.

    I know I've left some things a little vague in this explanation, if you try to use these classes and you can't figure something out, let me know.

    Thanks,
    Grayson Myers [MSFT]

    Wednesday, June 15, 2005 11:51 PM
  • Thanks for the great info!
    The first technique (CreateBitmap) works great.
    I just have a little quesion about the second technique...
    Do i have to create my own object that implements the IServiceProvider?
    I have tried passing the ServiceProvider property of CommandSet object but it only closes the diagram without exporting it...
    Mike
    Thursday, June 16, 2005 5:53 PM
  • In theory, the IServiceProvider passed to the CommandSet should work just fine.  I just tried this with the same results as you got, though.  Unfortunately there's a bug here that's preventing the DiagramExporter class from working with designers created using the DSL tools.  Please feel free to log this in the MSDN product feedback center, then you'll be able to track progress.

    In the meantime, you can continue to use the first method.

    Thanks,
    Grayson
    Tuesday, June 21, 2005 3:56 PM
  • Hi Grayson,

    I need to generate a .cd file and convert it to a class diagram image programatically. I investigated anything I could find on the web related to Microsoft.VisualStudio and its sub namespace and classes.

    Can you please guide me how to achieve a proper ClassDiagram object from the .cd (xml) file? (something that I can just call its CreateBitmap method...)

    I really appreciate your help.

    Regards

    Paymon

    info@paymon.net

     

    Wednesday, February 22, 2006 1:34 PM
  • Hi All,

     

    I would like to write a small add-in for Visual Studio to automaticly export all diagrams as images. I have read a lot of interesting things on this post, but I have a problem with both classes DiagramExporter and Diagram (CreateBitmap).

     

    In an add-in, can I have a IServiceProvider need in DiagramExporter constructor ?

    How can I retreive a Diagram object (abstract class) ?

     

    If you have some example of code, it will be very nice.

     

    Thanks in advance for your help.

     

    François.

    Monday, November 5, 2007 4:35 PM
  • A year too late, and it should probably be in another thread, but for the sake of answering, you can get the Diagram object with :

                

     

    Diagram currentDiagram = null;

     

    foreach (DiagramClientView view in MyCompany.MyApplication.MyApplicationDiagram.view)

    {

     

    if ((view.Diagram != null) && (view.Diagram.Store == my_current_store))

    {

    currentDiagram = view.Diagram;

     

    break;

    }

    }


     

    if (currentDiagram == null)

     

    return;

    Corrections : I was mistaken about what a "DiagramClientView" is. It contains an object for all file ever opened by the application (even closed ones). To find the correct diagram, you can do a search until a match is found (I do here a match with the store). Note that the diagram can be null if a file is closed.
    • Edited by MLavallee Wednesday, August 12, 2009 1:45 PM Errata
    Tuesday, August 11, 2009 3:22 PM