3

I have two class diagrams in PlantUML and would like to define a class in one file and reuse it in the other. See the example below.

diagram1.puml:

@startuml diagram1

class "Foo" as foo {
 ...attributes
}

@enduml

and diagram2:

@startuml diagram2

!include diagram1!foo

class "Bar" as bar {
 ...attributes
}

@enduml

Now my expectation is that diagram2 will show me both classes Foo and Bar in the diagram. However, the included class is not shown in the render.

How can this be solved?

Saffie
  • 445
  • 3
  • 15
  • 1
    Do you get a error message? What happens when you remove the `@startuml` and `@enduml` from the first diagram? – albert May 10 '21 at 18:04
  • @albert I do not get an error message. I just do not see anything included visually. When I remove `@startuml` and `@enduml` then it actually works! – Saffie May 12 '21 at 07:08
  • As far as I know the include statement includes code as if it was typed in that included place, so I can imagine that the original doesn't work. – albert May 12 '21 at 07:58
  • If you post it as an answer then I can accept it for you. – Saffie May 25 '21 at 14:00

1 Answers1

3

The plantuml include statement includes code as if it was typed in that included place (like include files for e.g C / C++ etc. do too). So the code should be like: For Diagram2:

@startuml diagram2

!include diagram1!foo

class "Bar" as bar {
 ...attributes
}

@enduml

and for diagram1

class "Foo" as foo {
 ...attributes
}

When you want also show diagram1 as a separate file you have to add an extra file with:

@startuml diagram1_show

!include diagram1!foo

@enduml
albert
  • 8,285
  • 3
  • 19
  • 32