internal static void TranslateDiagram(EntityDesignerDiagram diagram, DesignerModel.Diagram modelDiagram) { var viewModel = diagram.ModelElement; viewModel.ModelXRef.Add(modelDiagram, diagram, viewModel.EditingContext); using (var t = diagram.Store.TransactionManager.BeginTransaction("Translate diagram", true)) { // list of shapes that don't have corresponding element in model and require auto-layout var shapesToAutoLayout = new List<ViewModelDiagram.ShapeElement>(); // try to find object in model for each shape on a diagram foreach (var shapeElement in diagram.NestedChildShapes) { var entityShape = shapeElement as EntityTypeShape; if (entityShape != null && entityShape.ModelElement != null) { var modelEntity = viewModel.ModelXRef.GetExisting(entityShape.ModelElement) as ModelEntityType; if (modelEntity != null) { var modelEntityTypeShape = modelDiagram.EntityTypeShapes.FirstOrDefault(ets => ets.EntityType.Target == modelEntity); if (modelEntityTypeShape != null) { viewModel.ModelXRef.Add(modelEntityTypeShape, entityShape, viewModel.EditingContext); var rectangle = new ViewModelDiagram.RectangleD( modelEntityTypeShape.PointX.Value, modelEntityTypeShape.PointY.Value , modelEntityTypeShape.Width.Value, 0.0); entityShape.AbsoluteBounds = rectangle; entityShape.IsExpanded = modelEntityTypeShape.IsExpanded.Value; entityShape.FillColor = modelEntityTypeShape.FillColor.Value; } } if (viewModel.ModelXRef.GetExisting(entityShape) == null) { shapesToAutoLayout.Add(entityShape); } continue; } var associationConnector = shapeElement as AssociationConnector; if (associationConnector != null && associationConnector.ModelElement != null) { var modelAssociation = viewModel.ModelXRef.GetExisting(associationConnector.ModelElement) as ModelAssociation; if (modelAssociation != null) { var modelAssociationConnector = modelDiagram.AssociationConnectors.FirstOrDefault(ac => ac.Association.Target == modelAssociation); if (modelAssociationConnector != null) { viewModel.ModelXRef.Add(modelAssociationConnector, associationConnector, viewModel.EditingContext); TranslateAssociationConnectors(associationConnector, modelAssociationConnector, shapesToAutoLayout); } } continue; } var inheritanceConnector = shapeElement as InheritanceConnector; if (inheritanceConnector != null && inheritanceConnector.ModelElement != null) { var entityTypeBase = viewModel.ModelXRef.GetExisting(inheritanceConnector.ModelElement) as EntityTypeBaseType; var modelEntity = entityTypeBase.Parent as ModelEntityType; if (modelEntity != null) { var modelInheritanceConnector = modelDiagram.InheritanceConnectors.FirstOrDefault(ic => ic.EntityType.Target == modelEntity); if (modelInheritanceConnector != null) { viewModel.ModelXRef.Add(modelInheritanceConnector, inheritanceConnector, viewModel.EditingContext); TranslateInheritanceConnectors(inheritanceConnector, modelInheritanceConnector, shapesToAutoLayout); } } continue; } } diagram.AutoLayoutDiagram(shapesToAutoLayout); // initiate zoom level, grid and scalar property options diagram.ZoomLevel = modelDiagram.ZoomLevel.Value; diagram.ShowGrid = modelDiagram.ShowGrid.Value; diagram.SnapToGrid = modelDiagram.SnapToGrid.Value; diagram.DisplayNameAndType = modelDiagram.DisplayType.Value; diagram.DiagramId = modelDiagram.Id.Value; diagram.Title = modelDiagram.Name.Value; t.Commit(); } }
public void TransactionCommit(EntityDesignerDiagram diagram) { //If this is a Drag & Drop from the SE transaction, then arrange the new elements if (_autoArrangeInfo.Tracking) { if (_autoArrangeInfo.Objects.Count > 0 || _autoArrangeInfo.HiddenObjects.Count > 0) { //Arrange the new elemenst before the transaction finishes: using (var t = diagram.Store.TransactionManager.BeginTransaction(Resources.Tx_LayoutDiagram)) { //Place single object where the user dropped, and multiple get autoarranged if (_autoArrangeInfo.Objects.Count > 0) { //Place the first entity-type-shape to where the user released the mouse and auto layout the rest. var haveDropPoint = (!_autoArrangeDropPoint.IsEmpty); var shapesToAutoLayout = new List<ShapeElement>(); var hasSetUserDefinedPosition = false; foreach (var shape in _autoArrangeInfo.Objects) { var firstShape = shape as EntityTypeShape; if (haveDropPoint && firstShape != null && !hasSetUserDefinedPosition) { firstShape.Location = _autoArrangeDropPoint; hasSetUserDefinedPosition = true; } else { shapesToAutoLayout.Add(shape); } } if (shapesToAutoLayout.Count > 0) { diagram.AutoLayoutDiagram(shapesToAutoLayout); } t.Commit(); } _autoArrangeInfo.Objects.Clear(); } //Add hidden objects to 0,0 if (_autoArrangeInfo.HiddenObjects.Count > 0) { foreach (var se in _autoArrangeInfo.HiddenObjects) { var cs = se as EntityTypeShape; cs.Location = new PointD(0, 0); } _autoArrangeInfo.HiddenObjects.Clear(); } } } }