File size: 21,475 Bytes
10b2298 |
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
' ========================================================================
' Plantuml : a free UML diagram generator
' ========================================================================
'
' (C) Copyright 2009, Arnaud Roques
'
' Project Info: http://plantuml.sourceforge.net
'
' This file is part of Plantuml.
'
' Plantuml is free software; you can redistribute it and/or modify it
' under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' Plantuml distributed in the hope that it will be useful, but
' WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
' or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
' License for more details.
'
' You should have received a copy of the GNU General Public
' License along with this library; if not, write to the Free Software
' Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
' USA.
'
' [Java is a trademark or registered trademark of Sun Microsystems, Inc.
' in the United States and other countries.]
'
' Original Author: Arnaud Roques
' Word Macro: Alain Bertucat
'
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Const startuml = "@startuml"
Const enduml = "@enduml"
Function getJarPath() As String
Set fs = CreateObject("Scripting.FileSystemObject")
nbTemplates = ActiveDocument.Parent.Templates.Count
mainPath = ActiveDocument.Path
try = ActiveDocument.Path & "\"
nb = InStrRev(mainPath, "\")
Do While nb > 1 And fs.FileExists(mainPath + "\plantuml.jar") = False
mainPath = Left(mainPath, nb - 1)
try = try & vbCrLf & mainPath & "\"
nb = InStrRev(mainPath, "\")
Loop
For i = 1 To nbTemplates
If fs.FileExists(mainPath + "\plantuml.jar") = False Then
mainPath = ActiveDocument.Parent.Templates.Item(i).Path
try = try & vbCrLf & ActiveDocument.Parent.Templates.Item(i).Path & "\"
nb = InStrRev(mainPath, "\")
Do While nb > 1 And fs.FileExists(mainPath + "\plantuml.jar") = False
mainPath = Left(mainPath, nb - 1)
try = try & vbCrLf & mainPath & "\"
nb = InStrRev(mainPath, "\")
Loop
End If
Next i
If fs.FileExists(mainPath + "\plantuml.jar") Then
getJarPath = mainPath
Else
getJarPath = "Error : Cannot find plantuml.jar in :" & vbCrLf & try
End If
End Function
' =========================================================
Sub ShowPlantumlJarPath()
Set fs = CreateObject("Scripting.FileSystemObject")
jarPath = getJarPath()
If fs.FileExists(jarPath & "\plantuml.jar") Then
MsgBox "OK : " & jarPath & "\plantuml.jar"
Else
MsgBox jarPath
End If
End Sub
' =========================================================
Sub RemoveOldVersionPlantUMLSyles()
On Error GoTo DeleteEnd
ActiveDocument.Styles("PlantUML").Delete
On Error GoTo 0
DeleteEnd:
On Error GoTo 0
Call Macro_UML_all
End Sub
' =========================================================
Sub Macro_UML_all()
Macro_UML ("all")
End Sub
' =========================================================
Function Macro_UML_parg()
Macro_UML ("parg")
End Function
' =========================================================
Function Macro_UML(scope)
'
' Macro_UML : generation de diagramme dans un document a partir de la description
' "textuelle" de ces diagrammes dans le document
' Macro creee le 31/03/2009 par bertucat_a
'
' - Initialisations
'
Call ToolbarInit
Set statusButton = CommandBars("UML").Controls(6)
Call CreateStyle
Call CreateStyleImg
Call ShowPlantuml
'
' --- Reperage debut et fin de description de schema
'
Call ShowHiddenText
Selection.Range.Select
'
' --- Identifiant fichier sans extension et repertoire courant
'
documentId = ActiveDocument.Name
documentId = Left(documentId, Len(documentId) - 4)
' MSR - Gestion de l existence du module java
' Verification du fichier plantuml.jar
Set fs = CreateObject("Scripting.FileSystemObject")
jarPath = getJarPath()
If fs.FileExists(jarPath & "\plantuml.jar") = False Then
MsgBox jarPath
GoTo Macro_UML_exit
End If
'
' - Phase 1 : Creation d'un fichier texte par bloc de description de schema
' Recherche debut de bloc : "@startuml"
' Fichier texte ouvert en "arriere-plan" (visible:=false)
' Nom suffixe par numero d'ordre sur 4 digits
' Chaque paragraphe d'un bloc est mis en style "UML"
' puis le bloc est copie/colle dans le fichier texte
'
statusButton.Caption = "Extract"
statusButton.Visible = False
statusButton.Visible = True
If scope = "all" Then
Set parsedText = ActiveDocument.Content
isForward = True
Else
Set parsedText = Selection.Range
parsedText.Collapse
isForward = False
End If
parsedText.Find.Execute FindText:=startuml, Forward:=isForward
If parsedText.Find.Found = True Then
'MSR - on garde pour premiere ligne uniquement "@plantuml" avec le retour de ligne !
Set singleParagraph = parsedText.Paragraphs(1).Range
singleParagraph.Collapse
Else
GoTo Macro_UML_exit
End If
Do While parsedText.Find.Found = True And _
(scope = "all" Or currentIndex < 1)
statusButton.Caption = "Extract." & currentIndex + 1
statusButton.Visible = False
statusButton.Visible = True
Set currentParagraph = parsedText.Paragraphs(1)
Set paragraphRange = currentParagraph.Range
paragraphRange.Collapse
jobDone = False
Do Until jobDone
If Left(currentParagraph.Range.Text, Len(startuml)) = startuml Then
Set paragraphRange = currentParagraph.Range
paragraphRange.Collapse
End If
paragraphRange.MoveEnd Unit:=wdParagraph
If Left(currentParagraph.Range.Text, Len(enduml)) = enduml Then
paragraphRange.Style = "PlantUML"
paragraphRange.Copy
Set textFile = Documents.Add(Visible:=False)
textFile.Content.Paste
currentIndex = currentIndex + 1
textFileId = documentId & "_extr" & Right("000" & currentIndex, 4) & ".txt"
textFile.SaveAs FileName:=jarPath & "\" & textFileId, FileFormat:=wdFormatText
textFile.Close
jobDone = True
End If
Set currentParagraph = currentParagraph.Next
If currentParagraph Is Nothing Then
jobDone = True
End If
Loop
parsedText.Collapse Direction:=wdCollapseEnd
If scope = "all" Then
parsedText.Find.Execute FindText:=startuml, Forward:=True
End If
Loop
'
' - Creation d'un fichier de reservation qui sera detruit a la fin de la procedure Java
'
statusButton.Caption = "Gener"
statusButton.Visible = False
statusButton.Visible = True
Set lockFile = Documents.Add(Visible:=False)
lockFile.SaveAs FileName:=jarPath & "\javaumllock.tmp", FileFormat:=wdFormatText
lockFile.Close
'
' - Call to PlantUML to generate images from text descriptions
'
javaCommand = "java -classpath """ & jarPath & "\plantuml.jar;" & _
jarPath & "\plantumlskins.jar"" net.sourceforge.plantuml.Run -word """ & jarPath & """"
Shell (javaCommand)
' This sleep is needed, but we don't know why...
Sleep 500
'
' - Phase 2 : Insertion des schemas dans le documents apres validation de leur creation
' (boite de dialogue : la fenetre de commandes DOS doit etre refermee ou bien
' suppression par traitement Java du fichier javaumllock.temp)
' Insertion apres chaque paragraphe de fin de bloc de description
' (autant de fois que de blocs detectes en phase 1,
' recherche de la chaine de reperage de fin de bloc)
' Si une image est deja rattachee au paragraphe suivant et si ce paragraphe
' est en style "PlantUML", le paragraphe est supprime
' Un nouveau paragraphe (style "PlantUML", non cache) est cree pour recevoir l'image
' Nom image idem fichier texte, avec extension .png (genere par utilitaire)
' Gestion d'une boucle pour le cas o� plusieurs images sont generees pour un shema
'
jobDone = False
currentIndex = 0
Do
currentIndex = currentIndex + 1
statusButton.Caption = "Gener." & currentIndex
statusButton.Visible = False
statusButton.Visible = True
DoEvents
Sleep 1000
If fs.FileExists(jarPath & "\javaumllock.tmp") = False Then
jobDone = True
Exit Do
End If
If currentIndex > 30 Then
statusButton.Visible = False
MsgBox ("Java Timeout. Aborted.")
Exit Do
End If
Loop
If jobDone = False Then
End
End If
statusButton.Caption = "Inser"
statusButton.Visible = False
statusButton.Visible = True
If scope = "all" Then
Set parsedText = ActiveDocument.Content
isForward = True
Else
Set parsedText = singleParagraph
isForward = True
End If
parsedText.Find.Execute FindText:=enduml, Forward:=isForward
currentIndex = 0
Do While parsedText.Find.Found = True And (scope = "all" Or currentIndex < 1)
currentIndex = currentIndex + 1
statusButton.Caption = "Inser." & currentIndex
statusButton.Visible = False
statusButton.Visible = True
On Error GoTo LastParagraph
Set currentParagraph = parsedText.Paragraphs(1).Next.Range
Do While currentParagraph.InlineShapes.Count > 0 And currentParagraph.Style = "PlantUMLImg"
currentParagraph.Delete
Set currentParagraph = parsedText.Paragraphs(1).Next.Range
Loop
On Error GoTo 0
Set currentRange = currentParagraph
imagesDirectory = jarPath & "\" & documentId & "_extr" & Right("000" & currentIndex, 4) & "*.png"
image = Dir(imagesDirectory)
While image <> ""
Set currentParagraph = ActiveDocument.Paragraphs.Add(Range:=currentRange).Range
Set currentRange = currentParagraph.Paragraphs(1).Next.Range
currentParagraph.Style = "PlantUMLImg"
currentParagraph.Collapse
Set image = currentParagraph.InlineShapes.AddPicture _
(FileName:=jarPath & "\" & image _
, LinkToFile:=False, SaveWithDocument:=True)
image.AlternativeText = "Generated by PlantUML"
If image.ScaleHeight > 100 Or image.ScaleWidth > 100 Then
image.Reset
End If
image = Dir()
Wend
parsedText.Collapse Direction:=wdCollapseEnd
parsedText.Find.Execute FindText:=enduml, Forward:=True
Loop
'
' - Phase 3 : suppression des fichiers de travail (texte et PNG)
'
Phase3:
statusButton.Caption = "Delete"
statusButton.Visible = False
statusButton.Visible = True
On Error Resume Next
Kill (jarPath & "\" & documentId & "_extr*.*")
On Error GoTo 0
'MSR - gestion du bouton afficher/masquer
Macro_UML_exit:
statusButton.Visible = False
' on affiche le text
Call ShowHiddenText
Exit Function
LastParagraph:
Selection.EndKey Unit:=wdStory
Selection.TypeParagraph
Selection.ClearFormatting
imagesDirectory = jarPath & "\" & documentId & "_extr" & Right("000" & currentIndex, 4) & "*.png"
image = Dir(imagesDirectory)
While image <> ""
Set currentParagraph = ActiveDocument.Paragraphs.Add.Range
Set currentRange = currentParagraph.Paragraphs(1).Next.Range
currentParagraph.Style = "PlantUMLImg"
currentParagraph.Collapse
Set image = currentParagraph.InlineShapes.AddPicture _
(FileName:=jarPath & "\" & image _
, LinkToFile:=False, SaveWithDocument:=True)
image.AlternativeText = "Generated by PlantUML"
If image.ScaleHeight > 100 Or image.ScaleWidth > 100 Then
image.Reset
End If
image = Dir()
Wend
'Resume Next
GoTo Phase3
End Function
' =========================================================
Function ToolbarInit()
On Error GoTo ToolbarCreation
Set toolBar = ActiveDocument.CommandBars("UML")
On Error GoTo 0
toolBar.Visible = True
On Error GoTo ButtonAdd
Set currentButton = toolBar.Controls(1)
On Error GoTo 0
currentButton.OnAction = "Module1.SwitchP"
currentButton.Style = msoButtonCaption
currentButton.Caption = Chr(182)
currentButton.Visible = True
On Error GoTo ButtonAdd
Set currentButton = toolBar.Controls(2)
On Error GoTo 0
currentButton.OnAction = "Module1.ShowPlantuml"
currentButton.Style = msoButtonCaption
currentButton.Caption = "Show PlantUML"
currentButton.Visible = True
On Error GoTo ButtonAdd
Set currentButton = toolBar.Controls(3)
On Error GoTo 0
currentButton.OnAction = "Module1.HidePlantuml"
currentButton.Style = msoButtonCaption
currentButton.Caption = "Hide PlantUML"
currentButton.Visible = True
On Error GoTo ButtonAdd
Set currentButton = toolBar.Controls(4)
On Error GoTo 0
currentButton.OnAction = "Module1.Macro_UML_all"
currentButton.Style = msoButtonCaption
currentButton.Caption = "UML.*"
currentButton.Visible = True
On Error GoTo ButtonAdd
Set currentButton = toolBar.Controls(5)
On Error GoTo 0
currentButton.OnAction = "Module1.Macro_UML_parg"
currentButton.Style = msoButtonCaption
currentButton.Caption = "UML.1"
currentButton.Visible = True
On Error GoTo ButtonAdd
Set currentButton = toolBar.Controls(6)
On Error GoTo 0
currentButton.OnAction = ""
currentButton.Style = msoButtonCaption
currentButton.Caption = "Trace"
currentButton.Visible = True
Exit Function
ToolbarCreation:
Set toolBar = ActiveDocument.CommandBars.Add(Name:="UML")
Resume Next
ButtonAdd:
Set currentButton = toolBar.Controls.Add(Type:=msoControlButton, Before:=toolBar.Controls.Count + 1)
Resume Next
End Function
' =========================================================
Function CreateStyle()
On Error GoTo CreateStyleAdding
Set myStyle = ActiveDocument.Styles("PlantUML")
Exit Function
CreateStyleAdding:
Set myStyle = ActiveDocument.Styles.Add(Name:="PlantUML", Type:=wdStyleTypeParagraph)
myStyle.BaseStyle = "Normal"
myStyle.AutomaticallyUpdate = True
With myStyle.Font
.Name = "Courier New"
.Size = 9
.Hidden = True
.Color = wdColorGreen
End With
With myStyle.ParagraphFormat
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorLightGreen
End With
.LeftIndent = CentimetersToPoints(0)
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = 12254650
End With
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleDashLargeGap
.LineWidth = wdLineWidth050pt
.Color = 3910491
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleDashLargeGap
.LineWidth = wdLineWidth050pt
.Color = 3910491
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleDashLargeGap
.LineWidth = wdLineWidth050pt
.Color = 3910491
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleDashLargeGap
.LineWidth = wdLineWidth050pt
.Color = 3910491
End With
With .Borders
.DistanceFromTop = 1
.DistanceFromLeft = 4
.DistanceFromBottom = 1
.DistanceFromRight = 4
.Shadow = False
End With
End With
' ajout des tabulations
myStyle.NoSpaceBetweenParagraphsOfSameStyle = False
myStyle.ParagraphFormat.TabStops.ClearAll
myStyle.ParagraphFormat.TabStops.Add Position:= _
CentimetersToPoints(1), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
myStyle.ParagraphFormat.TabStops.Add Position:= _
CentimetersToPoints(2), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
myStyle.ParagraphFormat.TabStops.Add Position:= _
CentimetersToPoints(3), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
myStyle.ParagraphFormat.TabStops.Add Position:= _
CentimetersToPoints(4), Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
End Function
' =========================================================
Function CreateStyleImg()
On Error GoTo CreateStyleImgAdding
Set myStyle = ActiveDocument.Styles("PlantUMLImg")
On Error GoTo 0
Exit Function
CreateStyleImgAdding:
Set myStyle = ActiveDocument.Styles.Add(Name:="PlantUMLImg", Type:=wdStyleTypeParagraph)
myStyle.BaseStyle = "Normal"
myStyle.AutomaticallyUpdate = True
End Function
' =========================================================
' MSR - gestion de l'option d'affichage des textes masques du style : "PlantUML"
Function ShowPlantuml()
'WordBasic.ShowComments
' On positionne un repere pour y retourner apres traitement
ActiveDocument.Bookmarks.Add Name:="Position", Range:=Selection.Range
Set myStyle = ActiveDocument.Styles("PlantUML")
Set toolBar = ActiveDocument.CommandBars("UML")
toolBar.Controls(2).Visible = False
toolBar.Controls(3).Visible = True
toolBar.Controls(4).Visible = True
toolBar.Controls(5).Visible = True
Call ShowHiddenText
'On se repostionne sur le repere et on l'efface
Selection.GoTo What:=wdGoToBookmark, Name:="Position"
ActiveDocument.Bookmarks(Index:="Position").Delete
End Function
' =========================================================
' MSR - gestion de l'option d'affichage des textes masques du style : "PlantUML"
Function HidePlantuml()
'WordBasic.ShowComments
' On positionne un repere pour y retourner apres traitement
ActiveDocument.Bookmarks.Add Name:="Position", Range:=Selection.Range
Set myStyle = ActiveDocument.Styles("PlantUML")
Set toolBar = ActiveDocument.CommandBars("UML")
toolBar.Controls(2).Visible = True
toolBar.Controls(3).Visible = False
toolBar.Controls(4).Visible = False
toolBar.Controls(5).Visible = False
Call HideHiddenText
'On se repostionne sur le repere et on l'efface
Selection.GoTo What:=wdGoToBookmark, Name:="Position"
ActiveDocument.Bookmarks(Index:="Position").Delete
End Function
' =========================================================
Function HideHiddenText()
ActiveDocument.ActiveWindow.View.ShowAll = False
ActiveDocument.ActiveWindow.View.ShowHiddenText = False
End Function
' =========================================================
Function ShowHiddenText()
ActiveDocument.ActiveWindow.View.ShowAll = False
ActiveDocument.ActiveWindow.View.ShowHiddenText = True
End Function
' =========================================================
Function SwitchP()
flag = Not (ActiveDocument.ActiveWindow.View.ShowTabs)
ActiveDocument.ActiveWindow.View.ShowParagraphs = flag
ActiveDocument.ActiveWindow.View.ShowTabs = flag
ActiveDocument.ActiveWindow.View.ShowSpaces = flag
ActiveDocument.ActiveWindow.View.ShowHyphens = flag
ActiveDocument.ActiveWindow.View.ShowAll = False
End Function
' =========================================================
' MSR ajout de cette fonction pour ajouter une ligne (paragraphe) avant et apres les bornes
' Cette fonction permet de gerer le cas particulier de la demo : pas d'autre texte que le code plantuml dans le document
Function controlParagraphe()
If Left(ActiveDocument.Paragraphs(1).Range.Text, Len(startuml)) = startuml Then
ActiveDocument.Paragraphs(1).Range.Select
Selection.InsertParagraphBefore
ActiveDocument.Paragraphs(1).Range.Select
Selection.ClearFormatting
End If
If Left(ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range.Text, Len(enduml)) = enduml Then
ActiveDocument.Paragraphs.Add
ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range.Select
Selection.ClearFormatting
End If
If ActiveDocument.Paragraphs.Count > 1 Then
If Left(ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count - 1).Range.Text, Len(enduml)) = enduml Then
ActiveDocument.Paragraphs.Add
ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range.Select
Selection.ClearFormatting
End If
End If
End Function
|