Creating UML Class Diagrams programmatically

Uzamčený Creating UML Class Diagrams programmatically

  • 2012年2月2日 上午 10:54
     
     

    I want to be able to create class diagrams programmatically, and I saw VS Ultimate Edition has UML Class diagram support. However, it looks like its purpose is to create the class diagram describing existing program code, which is not what I want. I have implemented a UML object model which I would want to visualize, and also write a design userinterface for the class (in short, I have written a UML design tool without a user interface). So I need to be able do do, for intance, myDiagram.AddClassShape(myClass) and build the diagram from my object model. Can I do this with the Ultimate / Feature CK 2?

    Regards,

     

    Kim Johnsson

     


所有回覆

  • 2012年2月6日 上午 10:18
    版主
     
      包含代碼

    Hi Kim,

    Welcome to the MSDN Forum.

    Do you mean you want to generate the Class Diagram? As this picture:

    If so, you can write a xml file with the extension .cd because this file is just a XML file, and VS will generate the diagram according to this file.

    So you can implement the AddClassShape(myClass) like this:

        Public Sub AddClassShape(mytype As Type)
            Dim classnode = <Class Name=<%= mytype.FullName %> Collapsed="true">
                                <Position X="0.5" Y="1.75" Width="1.5"/>
                                <TypeIdentifier>
                                    <HashCode>AAAAAAAAACAAAAAAAACAAAACAAAAAAAAAAAAAAAAAAA=</HashCode>
                                    <FileName><%= mytype.Name %></FileName>
                                </TypeIdentifier>
                            </Class>
            ClassDiagramNode.Document.Add(classnode)
        End Sub
    

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • 2012年2月9日 下午 04:54
     
     

    Hi Mike!

    That's pretty nifty. However, that looks like the "standard" visual studio classdiagram, not the UML look I would expect the Ultimate Edition to have. What I need is something to runtime create UML-compatible class diagrams at will (with no connection to my program code). I just noticed there were UML Class diagram components visible in yhe Toolbox, but they were disabled. So basically I'm wondering whether I can create those shapes runtime and place them on some sort of diagram, or are they just there for the Ultimate Edition UML modeling capabilities to be used strictly as a tool within VS. Obviously there are commercial visual components that do this, but I'd rather just get the Ultimate Edition :-)

    Cheers,

    Kim

  • 2012年2月10日 上午 04:32
    版主
     
     已答覆 包含代碼

    Hi Kim,

    Do you mean this graph?

    UML

    If so, you can still try above way, it is still an xml file. Here is the sequenceDiagram file content:

    <?xml version="1.0" encoding="utf-8"?>
    <sequenceDesignerDiagram dslVersion="1.0.0.0" absoluteBounds="0, 0, 11, 8.5" name="UMLSequenceDiagram1">
      <SequenceDesignerModelMoniker Id="173746f0-a817-499c-a978-d82344168f22" />
      <nestedChildShapes>
        <lifelineShape Id="e215ab85-974e-4b71-9950-71a13019d0ad" absoluteBounds="2.8645833333333339, 1, 0.15, 7" visible="true" visualStyleMode="Modified">
          <lifelineMoniker Id="68e6da90-0ff0-4ff1-87b6-0d2dd95dbb88" LastKnownName="Lifeline1" />
          <relativeChildShapes>
            <umlLifelineHeadShape Id="8f982aac-7bc9-4284-91db-860af4d41250" absoluteBounds="2.4395833333333341, 0.6, 1, 0.4" customColor="White" visualStyleMode="Modified">
              <lifelineMoniker Id="68e6da90-0ff0-4ff1-87b6-0d2dd95dbb88" LastKnownName="Lifeline1" />
              <relativeChildShapes />
            </umlLifelineHeadShape>
            <lifelineHoverShape Id="482577c7-566f-4025-b408-f47fe4cb71b7" absoluteBounds="2.8645833333333339, 1, 0, 7">
              <lifelineMoniker Id="68e6da90-0ff0-4ff1-87b6-0d2dd95dbb88" LastKnownName="Lifeline1" />
            </lifelineHoverShape>
          </relativeChildShapes>
        </lifelineShape>
      </nestedChildShapes>
    </sequenceDesignerDiagram>

    If I misunderstood you, please feel free to follow up.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • 2012年2月10日 下午 09:16
     
     

    Hi Mike!

    Yeah, that's pretty much it. But just partly, I think.

    I'm sorry for my complete ignorance here, but can I do this runtime and interactively? Meaning I have my .exe running, and I build the XML programmatically, and show the resulting diagram in a diagram component which I have placed on a Form or a WPF Window? Could I write code to handle dragging and resizing class shapes, creating associations and other actions associated with editing a class diagram? Or would I operate on the XML structure, and just keep the reloading the resulting diagram after each edit?

    Or can I just create these diagrams as files to be viewed within the Visual Studio IDE?

    I don't have the Ultimate Edition currently, so I can't test this. I can just see the disabled UML components in the Toolbox.

    Cheers,

    Kim

  • 2012年2月13日 上午 03:15
    版主
     
     

    Hi Kim,

    Actually, I agree with your last option -- " just create these diagrams as files to be viewed within the Visual Studio IDE"

    When you have Ultimate Edition, please try this way.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • 2012年2月20日 上午 09:15
     
     

    Hi Mike,

    OK, thanks!

    Cheers,

    Kim

  • 2012年2月21日 上午 03:32
    版主
     
     

    Hi Kim,

    You are welcome.

    I have marked my reply as answer to close this thread. If you have any concerns on this thread, please feel free to unmark it and follow up.

    When you encounter problems in your further development, welcome to post on MSDN Forum, it is our pleasure to be a assistence.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • 2012年2月26日 下午 07:59
     
     

    OK, will do!

    Cheers,

    Kim