file [Solved] Copy of Element through command

3 years 2 months ago #4301 by kurales
I have found, how can I get the root Element:

MObject root = Modelio.getInstance().getModelingSession().getModel().getModelRoots().get(0);
Package rootPackage = ((Project) root).getModel();
and then
modelFactory.createComponent("Component", rootPackage, s);

But the created Element appears in the project-tree and not on the diagram. Is there something like
diagramHandle.unmask(newElement, rect.x, rect.y);
in this case?

Please Log in or Create an account to join the conversation.

3 years 2 months ago #4302 by kurales
I did it too:
IDiagramService diagramServices = Modelio.getInstance().getDiagramService();for (StaticDiagram diagram : session.findByClass(StaticDiagram.class)) {
 	            // if diagram context is root
 	          if (rootPackage.equals(diagram.getOrigin())) {

 	                // Get a IDiagramHandle for content manipulation.
 	                IDiagramHandle dh = diagramServices.getDiagramHandle(diagram);

 	                    dh.unmask(createdElement, 0, 0);
 	                    // Save all diagram modifications
 	                    dh.save();
 	
 	                    // Close the handle now it is no longer used
 	                    dh.close();
 	                }
 	          }

There is now only one question: how can I get bounds of the element?

Please Log in or Create an account to join the conversation.

3 years 2 months ago #4303 by kurales
For my previous question:
IDiagramNode dg = (IDiagramNode) dh.getDiagramGraphics(el).get(0); 
dh.unmask(createdElement, dg.getBounds().x+100, dg.getBounds().y);

And the last question: how can I get the list of stereotypes (or only one) of my host-element? I see only functions, which require the name of Stereotype, but this name I would like to get.

Please Log in or Create an account to join the conversation.

3 years 2 months ago #4304 by chm
Hi kurales,

to get names of all stereotypes applied on your model element, try this:
List<String> stereotypeNames = new ArrayList<>();
for (Stereotype s : el.getExtension()) {
  stereotypeNames.add(s.getName());
}

Hope this helps,


chm

Please Log in or Create an account to join the conversation.

3 years 2 months ago #4305 by kurales
Hi chm,

Thank you very much, it works!

Please Log in or Create an account to join the conversation.

3 years 2 months ago - 3 years 2 months 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:
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?

Please Log in or Create an account to join the conversation.

Moderators: tmachmaab
Time to create page: 0.081 seconds
^ Back to Top