Book a Demo

Author Topic: Placing Diagrams to Clipboard  (Read 10400 times)

philchudley

  • EA User
  • **
  • Posts: 750
  • Karma: +22/-0
  • EA Consultant / Trainer - Sparx Europe
    • View Profile
Placing Diagrams to Clipboard
« on: October 06, 2008, 11:29:02 pm »
I have developed an Add-In which produces documentation using Word. To place diagrams into this document I have used the following method from the Repository class

PutDiagramImageOnClipboard

This works just fine, however when the images are placed on the clipboard, they are opened and appear as diagrams.

I have noticed that when using the in-built RTF generator this does not happen. Is there anyway using an Add-In to place a diagram image on the clipboard without it opening ... just like the RFT generator.

In addition, once on the clipboar, a simple Paste in Word does the job, but large diagrams do not scale to fit the page in the word document. Is it possible to scale the diagram to the page size, before copying to the clipboard?

Cheers
Models are great!
Correct models are even greater!

philchudley

  • EA User
  • **
  • Posts: 750
  • Karma: +22/-0
  • EA Consultant / Trainer - Sparx Europe
    • View Profile
Re: Placing Diagrams to Clipboard
« Reply #1 on: October 06, 2008, 11:34:03 pm »
Correction to previous post:

Of course the method PutDiagramImageOnClipboard belongs to the Project class and not Repository as I stated before!
Models are great!
Correct models are even greater!

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Placing Diagrams to Clipboard
« Reply #2 on: October 06, 2008, 11:49:18 pm »
Hi Phil,

I don't know about scaling. I suspect without proof that the scaling settings in EA won't affect this behavior.

As to opening the diagram, you could be out of luck. EA draws some connectors - some or all of those that have only default settings - on the fly when a diagram is opened. That is, the diagram links are not stored in the database, but are inferred from the elements represented on the diagram. This behavior may require EA to construct the diagram before 'taking a picture' for the API.

Of course, the above logic would (or could, at least) apply to generating RTF reports as well. These reports, or certainly the WYSIWYG reports, are quite a bit newer though, having appeared in a later major version of EA than the API call. Perhaps they used a different code path. That's happened before in EA's history.

Perhaps it is worth submitting this as a bug report. After all, a feature is 'missing' from one code path. Whether this is through error or evolution is not the issue; if you can use it in one case and it does not work in the other, then it is a defect. [IM (in this case not so H) O]

David
« Last Edit: October 06, 2008, 11:49:42 pm by Midnight »
No, you can't have it!

philchudley

  • EA User
  • **
  • Posts: 750
  • Karma: +22/-0
  • EA Consultant / Trainer - Sparx Europe
    • View Profile
Re: Placing Diagrams to Clipboard
« Reply #3 on: October 07, 2008, 07:05:47 pm »
Hi David

Thanks, that confirms what I had suspected ... but it is always good to pose the questions in case there any hidden gems

Cheers
Phil
Models are great!
Correct models are even greater!

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Placing Diagrams to Clipboard
« Reply #4 on: October 07, 2008, 11:50:52 pm »
Phil,

Don't give up yet. This might not be a (completely) dead end. Here's the context:

A few years ago - this dates back to EA 5.x times, but the core issues remain relevant today - I developed an automation system that built a lot of diagrams, each was a viewpoint on a complex model. Many of the relationships in the overall model were inferred from information entered by users, so did not appear in the EA model, at least not on any diagrams. The application often had to illustrate specific relationships (often limited to specific types) between subsets of the various elements in the model.

We had quite a problem when we did this. After we created diagrams, users who opened them from the UI were presented with all associations between the elements in each diagram, not just the ones we had selected (as relevant for this viewpoint). Nothing we did seemed to solve this.

It turned out that this was a side effect of EA adding associations on the fly. Since there were no non-default attributes of the (in the current context) extraneous associations, EA did not store them as diagram links in the database. These associations were then added on the fly each time a user opened the diagram. Not good.

The solution was to add a check for additional associations that should not be shown on these diagram types. When one of these associations was found, we added a diagram link to the diagram though automation, and marked the link as not visible. By doing so we had added a non-standard attribute. Visibility is turned on by default, so turning it off is an explicit change. [Note that simply adding a diagram link to a diagram might not be enough to cause the link to be stored in the database. There must be some non-standard change to trigger EA to save a record. EA 'knows' it can recreate a link with default properties on the fly, so does not 'waste' resources to store the link.]

Once we did the above our diagrams were fine. Of course we also had to add tests when potentially troublesome new associations were added to the model. We did not have to test when associations were removed from the model; EA prunes extraneous diagram links from the database automatically.

Still with me?

So, perhaps you could do something like this. In your case you might need to explicitly add links, but you might handle both adding and hiding as your requirements dictate. You would have to set some non-default property of the diagram links in question. [Note that adding a non-default property to the connection underlying the diagram link will not work. The change needs to be on the diagram link itself.]

You could run this at various times, depending on how your application needs to work. Perhaps you could do so just before generating your diagram export. You might even have a cleanup routine that reversed the changes.

HTH, David
No, you can't have it!

Dermot

  • EA Administrator
  • EA User
  • *****
  • Posts: 591
  • Karma: +7/-0
    • View Profile
Re: Placing Diagrams to Clipboard
« Reply #5 on: October 09, 2008, 03:11:20 pm »
Pardon me butting in without reading all of Midnight's very generous reply, but here is some VB code that I think does do what you want - see pasteDiagram():

Sub DumpDiagrams(Package As Object, Ratio As Integer, bBorder As Boolean, ElementId As Long, Header As Boolean)

'______________________________________________
'
'Dump Diagrams
'
'______________________________________________
    Dim idx As Integer, diag As Ea.Diagram
    
    For Each diag In Package.Diagrams
    
        ' Check if the diagram is directly under the package - don't print if not
        If diag.ParentId = ElementId Then
             If Header Then
                 ' get the Package name and comments
                 WriteLin diag.Name, False, False, "H2"
                 WriteLin diag.Notes, False, False, "NS"
             End If
            
             ' Place the diagram on the document
             PasteDiagram diag.DiagramGUID, Ratio, bBorder

             WriteLin "Figure " + Str(iFigure) + ": " + diag.Name, True, False, "NS"
             iFigure = iFigure + 1
            
             EaRepos.CloseDiagram (diag.DiagramID)
            
        End If
    
    Next
    
    Set diag = Nothing
        
End Sub

Public Sub PasteDiagram(sDiagGuid As String, Ratio As Integer, bBorder As Boolean)

    Dim oProject As Ea.Project
    Dim CurrRange As Range
    
    Set CurrRange = WordApp.ActiveDocument.Content
    CurrRange.Collapse Direction:=wdCollapseEnd
    
    ' set up a project object to retrieve the graphics
    Set oProject = EaRepos.GetProjectInterface()
    
    'Copy diiagram from EA to the Clipboard
    WordApp.ActiveDocument.Content.InsertParagraphAfter
    
    ' Paste this diagram to the document from the project Object using guid from package
    If oProject.PutDiagramImageOnClipboard(sDiagGuid, 0) Then
        CurrRange.Paste
    End If
    
    If Ratio > 0 Then
        ' Selection.InlineShapes(1).Height * (Ratio / 100)
        WordApp.ActiveDocument.InlineShapes(iFigure - 1).LockAspectRatio = True
        WordApp.ActiveDocument.InlineShapes(iFigure - 1).Width = WordApp.ActiveDocument.InlineShapes(iFigure - 1).Width * Ratio
    End If
    
    If bBorder Then
        WordApp.ActiveDocument.InlineShapes(iFigure - 1).Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
        WordApp.ActiveDocument.InlineShapes(iFigure - 1).Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
        WordApp.ActiveDocument.InlineShapes(iFigure - 1).Borders(wdBorderRight).LineStyle = wdLineStyleSingle
        WordApp.ActiveDocument.InlineShapes(iFigure - 1).Borders(wdBorderTop).LineStyle = wdLineStyleSingle
    End If
    
    WordApp.ActiveDocument.Content.InsertParagraphAfter
    
    Set oProject = Nothing
    Set CurrRange = Nothing
      

End Sub

The full code for this is downloadable from the main Sparx web site dealing with Automation code:
http://www.sparxsystems.com.au/resources/developers/autint_vb.html

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Placing Diagrams to Clipboard
« Reply #6 on: October 09, 2008, 09:44:45 pm »
My take on this - having given the code a quick scan only - is that you can resize the diagram image, but only on the target application side. And of course that limits you to the capabilities of the target software involved.

Another downside is that you cannot write a general purpose routine for this and then use it for multiple targets. The target-facing part would need to be custom written for each target, and could have a feature set that varied across targets.

Still, that's a lot better than "no" as an answer.
No, you can't have it!

Stefan81827

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Placing Diagrams to Clipboard
« Reply #7 on: October 15, 2008, 07:51:55 pm »
Thanks to Dermot for the link to the plugin source code !
If only the plugin code was updated from Visual Basic 6 to Visual Studio 2005, since DLLs generated by VB6 will not be compatible with Vista.
Dermot, are there any plans to publish updated code ?

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Placing Diagrams to Clipboard
« Reply #8 on: October 15, 2008, 10:33:15 pm »
You could convert the code yourself. The conversion should not be too difficult.

If you are designing something that is to be called from EA, just remember to make the VS2005 project COM-visible. IIRC you can do that by either choosing the appropriate project template or by setting the assembly properties via the My Project dialog. You should not have to do this if you are only calling into EA, but it probably would not hurt to do so anyway.

Take a look at the first few sections of the add-in API and EA object model sections of the EA documentation. There are a few cautionary notes on ensuring that garbage collection runs and that you don't leave hanging references to EA internals. These are pretty much common sense, but it does not hurt to review them regardless.

David

[PS: The first time you dive into EA automation it can be a bit jarring. Don't panic. You will very quickly get the hang of how EA wants to communicate, and things will go smoothly from then on.]
No, you can't have it!

Stefan81827

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: Placing Diagrams to Clipboard
« Reply #9 on: October 15, 2008, 11:08:26 pm »
Thanks for these hints, David !
I have worked a couple of months to develop the published code into a documentation tool (via MS Word) that fits our needs.
My boss wanted a fast solution, and this was a decent way to go, even though I had never used VB6 before.
Therefore, I am actually not so much concerned about the few adaptations towards EA, but more the interface to applications (i.e. Word), and the OS environment, which has obviously changed substantially since the good ole days of VB6.
The upgrade wizard of VStudio2005 came up with 100+ issues, and few of them seemed to be trivial.
For instance, since VB6, Microsoft has changed their policy to strict typing, hence objects and variants no longer work the way they did in VB6.
This does even affect the EA interface regarding add-in menus with multiple entries. Me, not being an expert in Microsoft tools and their zillions of methods, had to give up.
Could all this be solved by fixing the COM visibility ?
It would just be great if Sparx could do this effort once for the entire community. One could even think of porting the whole thing to the Java API, which will hide the OS dependency forever.
BR,
Stefan
« Last Edit: October 15, 2008, 11:18:28 pm by otts »

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Placing Diagrams to Clipboard
« Reply #10 on: October 16, 2008, 07:56:27 am »
Hi Stefan,

I certainly empathize with the issue you are facing. For what it's worth, If I were doing a port of VBA code I would not attempt the upgrade wizard at all, there are just too many differences between the languages. I would simply rewrite the code along similar lines. [Actually, the previous comments should read a bit differently: I do sometimes have to do this, I do not use the wizard, and I do rewrite the code; my results have been good.]

Working with VBA code and EA is completely satisfactory though. In fact, one thing I really like about this combination is that I don't need to have a development environment handy. If the target system has MS Office - I've tested this with Office 97 to 2003 - then I can simply write the VBA in a Word document (for example). I don't actually have to use the document itself, I can simply use it to contain the code and allow me to trigger the automation project.

I've used that trick to create lightweight documentation tools for (highly) customized EA output. The document with the VBA code does not become the output document. Instead, it creates a new, empty, Word document and uses that as the target. That way the end product does not have to have any macro code, and can be distributed widely without the eventual user needing to have any specific security (or other) settings.

This method also works well with customized templates. I've used it to produce reports in formats dictated by corporate policy, international standards (i.e. ISO or IEEE project deliverables), or both.

David
No, you can't have it!