' ======================================================================== ' 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) Sub Macro_UML_all() Macro_UML ("all") End Sub Sub Macro_UML_parg() Macro_UML ("parg") End Sub Sub Macro_Masquer() Gestion_Masquer End Sub Function Macro_UML(l_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 ' Gestion_Barre Set w_but = CommandBars("UML").Controls(4) Gestion_Style ("") ' ' --- Reperage debut et fin de description de schema ' w_Diag_d = "@startuml" w_Diag_f = "@enduml" w_Diag_idr = False w_Diag_no = 0 Set f_Doc = ActiveDocument Set w_Crs = Selection.Range w_Hid = ActiveDocument.ActiveWindow.View.ShowHiddenText If w_Hid = False Then ActiveDocument.ActiveWindow.View.ShowHiddenText = True End If ' MSR - verif du cas particulier : un paragraphe Call controlParagraphe("@startuml", "@enduml") w_Crs.Select ' ' --- Identifiant fichier sans extension et repertoire courant ' w_Fdoc_id = f_Doc.Name w_Fdoc_id = Left(w_Fdoc_id, Len(w_Fdoc_id) - 4) w_Fdoc_dir = f_Doc.Path ' MSR - Gestion de l existance du module java ' Verification du fichier plantuml.jar Dim iRetVal As Integer 'w_Fdoc_dir_Install = Environ("PLANTUML_INSTALL ") 'If w_Fdoc_dir_Install <> "" Then ' w_Fdoc_dir = w_Fdoc_dir_Install 'End If 'str_test = "test -> " + w_Fdoc_dir 'MsgBox (str_test) Dim nbTemplates As Integer nbTemplates = ActiveDocument.Parent.Templates.Count If nbTemplates > 1 Then For i = 1 To nbTemplates 'MsgBox (ActiveDocument.Parent.Templates.Item(i).Name) If ActiveDocument.Parent.Templates.Item(i).Name = "PlantUML.dot" Then w_Fdoc_dir = ActiveDocument.Parent.Templates.Item(i).Path End If Next i End If 'ActiveDocument.AttachedTemplate ' chercher si "plantuml.jar" existe ! Set fs = CreateObject("Scripting.FileSystemObject") filespec = w_Fdoc_dir + "\plantuml.jar" If Not (fs.FileExists(filespec)) Then MsgBox ("Warning : not find plantuml.jar !") 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 ' w_but.Caption = "Extract" w_but.Visible = False w_but.Visible = True If l_scope = "all" Then Set w_Fnd_rng = ActiveDocument.Content w_Fwd = True Else Set w_Fnd_rng = w_Crs w_Fnd_rng.Collapse w_Fwd = False End If w_Fnd_rng.Find.Execute FindText:=w_Diag_d, Forward:=w_Fwd If w_Fnd_rng.Find.Found = True Then 'MSR - on garde pour premiere ligne uniquement "w_Diag_d" avec le retour de ligne ! 'w_Fnd_rng.Paragraphs(1).Range.Text = w_Diag_d & Right(w_Fnd_rng.Paragraphs(1).Range, 1) Set w_Deb_rng = w_Fnd_rng.Paragraphs(1).Range w_Deb_rng.Collapse Else GoTo Macro_UML_exit End If Do While w_Fnd_rng.Find.Found = True And _ (l_scope = "all" Or w_Diag_no < 1) w_but.Caption = "Extract." & w_Diag_no + 1 w_but.Visible = False w_but.Visible = True Set w_Prgph = w_Fnd_rng.Paragraphs(1) Set w_Txt_rng = w_Prgph.Range w_Txt_rng.Collapse w_Diag_idr = True Do Until w_Diag_idr = False If Left(w_Prgph.Range.Text, Len(w_Diag_d)) = w_Diag_d Then 'w_Prgph.Range.Text = w_Diag_d & vbCrLf Set w_Txt_rng = w_Prgph.Range w_Txt_rng.Collapse End If w_Txt_rng.MoveEnd Unit:=wdParagraph If Left(w_Prgph.Range.Text, Len(w_Diag_f)) = w_Diag_f Then 'w_Prgph.Range.Text = w_Diag_f & vbCrLf w_Txt_rng.Style = "PlantUML" w_Txt_rng.Copy Set f_Txt = Documents.Add(Visible:=False) f_Txt.Content.Paste w_Diag_no = w_Diag_no + 1 w_Ftxt_id = w_Fdoc_id & "_extr" & Right("000" & w_Diag_no, 4) & ".txt" f_Txt.SaveAs FileName:=w_Fdoc_dir & "\" & w_Ftxt_id, FileFormat:=wdFormatText f_Txt.Close w_Diag_idr = False End If Set w_Prgph = w_Prgph.Next If w_Prgph Is Nothing Then w_Diag_idr = False End If Loop w_Fnd_rng.Collapse Direction:=wdCollapseEnd If l_scope = "all" Then w_Fnd_rng.Find.Execute FindText:=w_Diag_d, Forward:=True End If Loop ' ' - Creation d'un fichier de reservation qui sera detruit a la fin de la procedure Java ' w_but.Caption = "Gener" w_but.Visible = False w_but.Visible = True Set f_Loc = Documents.Add(Visible:=False) f_Loc.SaveAs FileName:=w_Fdoc_dir & "\javaumllock.tmp", FileFormat:=wdFormatText f_Loc.Close ' ' - Appel de l'utilitaire de creation des schemas au format PNG a partir des fichiers texte ' w_Cmd = "java -classpath """ & w_Fdoc_dir & "\plantuml.jar;" & _ w_Fdoc_dir & "\plantumlskins.jar"" net.sourceforge.plantuml.Run -word """ & w_Fdoc_dir & """" Shell (w_Cmd) ' 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 ' w_suite_idr = 0 Set fso = CreateObject("Scripting.FileSystemObject") w_bctr = 0 Do w_bctr = w_bctr + 1 w_but.Caption = "Gener." & w_bctr w_but.Visible = False w_but.Visible = True DoEvents Sleep 1000 If fso.FileExists(w_Fdoc_dir & "\javaumllock.tmp") = False Then w_suite_idr = 1 Exit Do End If If w_bctr > 30 Then MsgBox ("Java Timeout. Aborted.") Exit Do End If Loop If w_suite_idr <> 1 Then End End If w_but.Caption = "Inser" w_but.Visible = False w_but.Visible = True If l_scope = "all" Then Set w_Fnd_rng = ActiveDocument.Content w_Fwd = True Else Set w_Fnd_rng = w_Deb_rng w_Fwd = True End If w_Fnd_rng.Find.Execute FindText:=w_Diag_f, Forward:=w_Fwd w_i1 = 0 Do While w_Fnd_rng.Find.Found = True And _ (l_scope = "all" Or w_i1 < 1) w_i1 = w_i1 + 1 w_but.Caption = "Inser." & w_i1 w_but.Visible = False w_but.Visible = True Set w_Prgph_rng = w_Fnd_rng.Paragraphs(1).Next.Range Do While w_Prgph_rng.InlineShapes.Count > 0 And w_Prgph_rng.Style = "PlantUML" w_Prgph_rng.Delete Set w_Prgph_rng = w_Fnd_rng.Paragraphs(1).Next.Range Loop Set w_Inser_rng = w_Prgph_rng w_Fpng_gen = w_Fdoc_dir & "\" & w_Fdoc_id & "_extr" & Right("000" & w_i1, 4) & "*.png" w_Fpng_id = Dir(w_Fpng_gen) While w_Fpng_id <> "" Set w_Prgph_rng = ActiveDocument.Paragraphs.Add(Range:=w_Inser_rng).Range Set w_Inser_rng = w_Prgph_rng.Paragraphs(1).Next.Range w_Prgph_rng.Style = "PlantUML" w_Prgph_rng.Font.Hidden = False w_Prgph_rng.Collapse Set w_Img = w_Prgph_rng.InlineShapes.AddPicture _ (FileName:=w_Fdoc_dir & "\" & w_Fpng_id _ , LinkToFile:=False, SaveWithDocument:=True) If w_Img.ScaleHeight > 100 Or w_Img.ScaleWidth > 100 Then w_Img.Reset End If w_Fpng_id = Dir() Wend w_Fnd_rng.Collapse Direction:=wdCollapseEnd w_Fnd_rng.Find.Execute FindText:=w_Diag_f, Forward:=True Loop ' ' - Phase 3 : suppression des fichiers de travail (texte et PNG) ' variante en commentaire : Generation et execution d'un fichier .bat ' pour suppression des fichiers de travail (texte et PNG) ' A partir d'un fichier "squelette" ' Fichiers de commandes ouverts en "arriere-plan" (visible:=false) ' --> particularisation avec le repertoire courant et l'identifiant generique ' des fichiers a supprimer ' w_but.Caption = "Delete" w_but.Visible = False w_but.Visible = True On Error Resume Next Kill (w_Fdoc_dir & "\" & w_Fdoc_id & "_extr*.*") On Error GoTo 0 'MSR - gestion du bouton afficher/masquer Macro_UML_exit: w_but.Visible = False Set w_Bar = ActiveDocument.CommandBars("UML") Set w_but = w_Bar.Controls(1) w_but.Visible = False If w_Hid = False Then ActiveDocument.ActiveWindow.View.ShowHiddenText = False w_but.Caption = "Show PlantUML" Else w_but.Caption = "Hide PlantUML" End If w_but.Visible = True ' w_Crs.Select ' MsgBox ("Traitement UML termine") ' on affiche le text ActiveDocument.ActiveWindow.View.ShowAll = False ActiveDocument.ActiveWindow.View.ShowHiddenText = True End Function Sub Gestion_Barre() On Error GoTo Barre_Creat Set w_Bar = ActiveDocument.CommandBars("UML") On Error GoTo 0 w_Bar.Visible = True On Error GoTo Bouton_Creat Set w_but = w_Bar.Controls(1) On Error GoTo 0 ' w_but.Type = msoControlButton w_but.OnAction = "Module1.Macro_Masquer" w_but.Style = msoButtonCaption w_but.Caption = "Hide PlantUML" w_but.Visible = True On Error GoTo Bouton_Creat Set w_but = w_Bar.Controls(2) On Error GoTo 0 ' w_but.Type = msoControlButton w_but.OnAction = "Module1.Macro_UML_all" w_but.Style = msoButtonCaption w_but.Caption = "UML.*" w_but.Visible = True On Error GoTo Bouton_Creat Set w_but = w_Bar.Controls(3) On Error GoTo 0 ' w_but.Type = msoControlButton w_but.OnAction = "Module1.Macro_UML_parg" w_but.Style = msoButtonCaption w_but.Caption = "UML.1" w_but.Visible = True On Error GoTo Bouton_Creat Set w_but = w_Bar.Controls(4) On Error GoTo 0 ' w_but.Type = msoControlButton w_but.OnAction = "" w_but.Style = msoButtonCaption w_but.Caption = "Trace" w_but.Visible = True Barre_Exit: Exit Sub Barre_Creat: Set w_Bar = ActiveDocument.CommandBars.Add(Name:="UML") Resume Next Bouton_Creat: Set w_but = w_Bar.Controls.Add(Type:=msoControlButton, Before:=w_Bar.Controls.Count + 1) Resume Next End Sub Sub Gestion_Style(testModif As String) 'Cette fonction permet de gerer le style PlantUML ' - Creation du style avec testModif="" ' - Modification du style (masquer) avec testModif=Hide ' - Re-initialisaiton du style (re-afficher) avec testModif=Show ' - Existe deja avec testModif="" -> pas de modif On Error GoTo Style_Creat Set w_Stl = ActiveDocument.Styles("PlantUML") On Error GoTo 0 ' MSR ajout d'un test pour gestion de l'affichage If testModif = "Hide PlantUML" Then GoTo Style_Modif ElseIf testModif = "Show PlantUML" Then GoTo Style_Init End If Style_Exit: Exit Sub Style_Creat: Set w_Stl = ActiveDocument.Styles.Add(Name:="PlantUML", Type:=wdStyleTypeParagraph) w_Stl.BaseStyle = "Normal" w_Stl.AutomaticallyUpdate = True Style_Init: With w_Stl.Font .Name = "Courier New" .Size = 9 .Hidden = True .Color = wdColorGreen End With With w_Stl.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 w_Stl.NoSpaceBetweenParagraphsOfSameStyle = _ False w_Stl.ParagraphFormat.TabStops.ClearAll w_Stl.ParagraphFormat.TabStops.Add Position:= _ CentimetersToPoints(1), Alignment:=wdAlignTabLeft, Leader:= _ wdTabLeaderSpaces w_Stl.ParagraphFormat.TabStops.Add Position:= _ CentimetersToPoints(2), Alignment:=wdAlignTabLeft, Leader:= _ wdTabLeaderSpaces w_Stl.ParagraphFormat.TabStops.Add Position:= _ CentimetersToPoints(3), Alignment:=wdAlignTabLeft, Leader:= _ wdTabLeaderSpaces w_Stl.ParagraphFormat.TabStops.Add Position:= _ CentimetersToPoints(4), Alignment:=wdAlignTabLeft, Leader:= _ wdTabLeaderSpaces Exit Sub Style_Modif: ' Modification du style pour ne pas afficher le code plantUML, le cadre et le fond Set w_Stl = ActiveDocument.Styles("PlantUML") With w_Stl.Font .Name = "Courier New" .Size = 9 .Hidden = True .Color = wdColorGreen End With 'Cas particulier pour debuguer ! MSR ?? If w_Stl.Font.Hidden <> -1 Then w_Stl.Font.Hidden = -1 End If With w_Stl.ParagraphFormat With .Shading .Texture = wdTextureNone .ForegroundPatternColor = wdColorAutomatic .BackgroundPatternColor = wdColorAutomatic End With .Borders(wdBorderLeft).LineStyle = wdLineStyleNone .Borders(wdBorderRight).LineStyle = wdLineStyleNone .Borders(wdBorderTop).LineStyle = wdLineStyleNone .Borders(wdBorderBottom).LineStyle = wdLineStyleNone End With End Sub ' MSR - gestion de l'option d'affichage des textes masques du style : "PlantUML" Sub Gestion_Masquer() ' On positionne un repere pour y retourner apres traitement ActiveDocument.Bookmarks.Add Name:="Position", Range:=Selection.Range ' Changement du nom du bouton Set w_Bar = ActiveDocument.CommandBars("UML") Set w_but = w_Bar.Controls(1) 'w_but.State = msoButtonDown w_but.State = msoButtonUp If w_but.Caption = "Hide PlantUML" Then w_but.Caption = "Show PlantUML" w_Bar.Controls(2).Visible = Flase w_Bar.Controls(3).Visible = Flase Else w_but.Caption = "Hide PlantUML" w_Bar.Controls(2).Visible = True w_Bar.Controls(3).Visible = True End If On Error GoTo Gestion_Masquer_Exit Set w_Stl = ActiveDocument.Styles("PlantUML") On Error GoTo 0 ' Gestion de l'option d'affiche des zones masquees de Word w_Hid = ActiveDocument.ActiveWindow.View.ShowHiddenText 'Verifier l'etat reel ! If w_Bar.Controls(2).Visible = True And w_Hid = True Then w_Hid = False End If If w_Bar.Controls(2).Visible = False And w_Hid = False Then w_Hid = True End If If w_Hid = False Then ' on affiche le fond et la bordure Gestion_Style ("Show PlantUML") ' on affiche le text ActiveDocument.ActiveWindow.View.ShowAll = False ActiveDocument.ActiveWindow.View.ShowHiddenText = True Else ' on n'affiche pas le fond et la bordure Gestion_Style ("Hide PlantUML") ' on n'affiche pas le text ActiveDocument.ActiveWindow.View.ShowAll = False ActiveDocument.ActiveWindow.View.ShowHiddenText = False End If 'Gestion_Etat Gestion_Masquer_Exit: 'On se repostionne sur le repere et on l'efface Selection.GoTo What:=wdGoToBookmark, Name:="Position" ActiveDocument.Bookmarks(Index:="Position").Delete End Sub ' 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 Sub controlParagraphe(nameDeb As String, nameFin As String) Dim test As Boolean If Left(ActiveDocument.Paragraphs(1).Range.Text, Len(nameDeb)) = nameDeb 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(nameFin)) = nameFin 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(nameFin)) = nameFin Then ActiveDocument.Paragraphs.Add ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range.Select Selection.ClearFormatting End If End If End Sub Sub Gestion_Etat() ' Gestion de l'option d'affiche des zones masquees de Word w_Hid = ActiveDocument.ActiveWindow.View.ShowHiddenText 'If ActiveDocument.Styles("PlantUML2") Then ' 'End If ' on affiche le text ' ActiveDocument.ActiveWindow.View.ShowAll = False ' ActiveDocument.ActiveWindow.View.ShowHiddenText = True ' Gestion_Style ("Show PlantUML") End Sub