- Posts: 20
- Thank you received: 0
×
Modelio 4.0.0 released (07 Nov 2019)
Modelio 4.0.0 has been released ( www.modelio.org/forum/4-announcements/45...-4-0-0-released.html )
[Solved] Copy of Element through command
6 years 1 month ago - 6 years 1 month ago #4306 by kurales
It works, but there is the following problem: when there is only one diagram in the project, then is all ok, but when I create another one, the new element does not appear. This occurs in both diagrams. If I remove one of them, then it works fine again.My code of the command:
I think there is some trouble in thise section of code:
But how can i get the right diagram in this case?
public boolean createBox(Component el, IModule module) {
IModuleContext ctx = module.getModuleContext();
IModelingSession session = ctx.getModelingSession();
IDiagramService diagramServices = ctx.getModelioServices().getDiagramService();
MObject root = session.getModel().getModelRoots().get(0);
Package rootPackage = ((Project) root).getModel();
try (ITransaction tr = session.createTransaction("Create box")) {
IUmlModel modelFactory = session.getModel();
String name = el.getName();
name = module.getModuleContext().getI18nSupport().getString(name);
List<String> stereotypeNames = new ArrayList<>();
for (Stereotype s : el.getExtension()) {
stereotypeNames.add(s.getName());
}
Stereotype stereotype;
stereotype = session.getMetamodelExtensions().getStereotype(stereotypeNames.get(0),Metamodel.getMClass("Component"));
Component createdElement = modelFactory.createComponent("Component", rootPackage, stereotype);
modelFactory.getDefaultNameService().setDefaultName((ModelElement) createdElement, name);
for (StaticDiagram diagram : session.findByClass(StaticDiagram.class)) {
if (rootPackage.equals(diagram.getOrigin())) {
IDiagramHandle dh = diagramServices.getDiagramHandle(diagram);
IDiagramNode dg = (IDiagramNode) dh.getDiagramGraphics(el).get(0);
List<IDiagramGraphic> graph = dh.unmask(createdElement, dg.getBounds().x+200, dg.getBounds().y);
IDiagramNode dnode = (IDiagramNode)graph.get(0);
dnode.setProperty("SHOWTAGS", "TRUE");
dh.save();
dh.close();
}
}
tr.commit();
return true;
} catch (RuntimeException e) {
module.getModuleContext().getLogService().error(e);.getModuleContext().getLogService().error(e);
return false;
}
}
I think there is some trouble in thise section of code:
for (StaticDiagram diagram : session.findByClass(StaticDiagram.class)) {
if (rootPackage.equals(diagram.getOrigin())) {
IDiagramHandle dh = diagramServices.getDiagramHandle(diagram);
But how can i get the right diagram in this case?
Last Edit: 6 years 1 month ago by kurales.
Please Log in or Create an account to join the conversation.
6 years 1 month ago #4309 by pan
Just to be sure, each diagram contains the Component "el"?
On this lineIf el is not represented in the diagram it could lead to an error.
On this line
IDiagramNode dg = (IDiagramNode) dh.getDiagramGraphics(el).get(0);
Please Log in or Create an account to join the conversation.
6 years 1 month ago #4310 by kurales
Hi pan,
no, I would like to copy the element inside of one diagram and want to know, which diagram ist it. So it must be checked, that the element, that I have choosed, belongs to one diagram, in which I perform the copy-operation. Then can I get the handler with this diagram in the statement IDiagramHandle dh = diagramServices.getDiagramHandle(diagram);
no, I would like to copy the element inside of one diagram and want to know, which diagram ist it. So it must be checked, that the element, that I have choosed, belongs to one diagram, in which I perform the copy-operation. Then can I get the handler with this diagram in the statement IDiagramHandle dh = diagramServices.getDiagramHandle(diagram);
Please Log in or Create an account to join the conversation.
6 years 1 month ago #4311 by pan
OK. There is 2 ways to do it.
1 - You can do:but be careful if you do dh.getDiagramGraphics(el).get(0) it could lead to an error if dh.getDiagramGraphics(el) returns an empty list.
2 - From the element itself you can retrieve the diagrams in which it is shown by using:View the Element javadoc
1 - You can do:
dh.getDiagramGraphics(el)
2 - From the element itself you can retrieve the diagrams in which it is shown by using:
element.getDiagramElement()
The following user(s) said Thank You: kurales
Please Log in or Create an account to join the conversation.
6 years 1 month ago #4313 by kurales
The second way ist what I needed. Thank you!
Please Log in or Create an account to join the conversation.
Time to create page: 0.040 seconds