public dynWorkspaceViewModel(dynWorkspaceModel model, DynamoViewModel vm) { _model = model; var nodesColl = new CollectionContainer { Collection = Nodes }; WorkspaceElements.Add(nodesColl); var connColl = new CollectionContainer { Collection = Connectors }; WorkspaceElements.Add(connColl); var notesColl = new CollectionContainer { Collection = Notes }; WorkspaceElements.Add(notesColl); //respond to collection changes on the model by creating new view models //currently, view models are added for notes and nodes //connector view models are added during connection _model.Nodes.CollectionChanged += Nodes_CollectionChanged; _model.Notes.CollectionChanged += Notes_CollectionChanged; _model.Connectors.CollectionChanged += Connectors_CollectionChanged; _model.PropertyChanged += ModelPropertyChanged; HideCommand = new DelegateCommand <object>(Hide, CanHide); CrossSelectCommand = new DelegateCommand <object>(CrossingSelect, CanCrossSelect); ContainSelectCommand = new DelegateCommand <object>(ContainSelect, CanContainSelect); SetCurrentOffsetCommand = new DelegateCommand <object>(SetCurrentOffset, CanSetCurrentOffset); NodeFromSelectionCommand = new DelegateCommand(CreateNodeFromSelection, CanCreateNodeFromSelection); SetZoomCommand = new DelegateCommand <object>(SetZoom, CanSetZoom); FindByIdCommand = new DelegateCommand <object>(FindById, CanFindById); AlignSelectedCommand = new DelegateCommand <string>(AlignSelected, CanAlignSelected); SelectAllCommand = new DelegateCommand(SelectAll, CanSelectAll); FindNodesFromSelectionCommand = new DelegateCommand(FindNodesFromSelection, CanFindNodesFromSelection); DynamoSelection.Instance.Selection.CollectionChanged += NodeFromSelectionCanExecuteChanged; // sync collections Nodes_CollectionChanged(null, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, _model.Nodes)); Connectors_CollectionChanged(null, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, _model.Connectors)); Notes_CollectionChanged(null, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, _model.Notes)); }
internal void SaveWorkspaceAsImage(string path) { var initialized = false; var bounds = new Rect(); double minX = 0.0, minY = 0.0; var dragCanvas = WpfUtilities.ChildOfType <DragCanvas>(this); var childrenCount = VisualTreeHelper.GetChildrenCount(dragCanvas); for (int index = 0; index < childrenCount; ++index) { var child = VisualTreeHelper.GetChild(dragCanvas, index); var firstChild = VisualTreeHelper.GetChild(child, 0); switch (firstChild.GetType().Name) { case "NodeView": case "NoteView": case "AnnotationView": break; // Until we completely removed InfoBubbleView (or fixed its broken // size calculation), we will not be including it in our size // calculation here. This means that the info bubble, if any, will // still go beyond the boundaries of the final PNG file. I would // prefer not to add this hack here as it introduces multiple issues // (including NaN for Grid inside the view and the fix would be too // ugly to type in). Suffice to say that InfoBubbleView is not // included in the size calculation for screen capture (work-around // should be obvious). // // case "InfoBubbleView": // child = WpfUtilities.ChildOfType<Grid>(child); // break; // We do not take anything other than those above // into consideration when the canvas size is measured. default: continue; } // Determine the smallest corner of all given visual elements on the // graph. This smallest top-left corner value will be useful in making // the offset later on. // var childBounds = VisualTreeHelper.GetDescendantBounds(child as Visual); minX = childBounds.X < minX ? childBounds.X : minX; minY = childBounds.Y < minY ? childBounds.Y : minY; childBounds.X = (double)(child as Visual).GetValue(Canvas.LeftProperty); childBounds.Y = (double)(child as Visual).GetValue(Canvas.TopProperty); if (initialized) { bounds.Union(childBounds); } else { initialized = true; bounds = childBounds; } } // Nothing found in the canvas, bail out. if (!initialized) { return; } // Add padding to the edge and make them multiples of two (pad 10px on each side). bounds.Width = 20 + ((((int)Math.Ceiling(bounds.Width)) + 1) & ~0x01); bounds.Height = 20 + ((((int)Math.Ceiling(bounds.Height)) + 1) & ~0x01); var currentTransformGroup = WorkspaceElements.RenderTransform as TransformGroup; WorkspaceElements.RenderTransform = new TranslateTransform(10.0 - bounds.X - minX, 10.0 - bounds.Y - minY); WorkspaceElements.UpdateLayout(); var rtb = new RenderTargetBitmap(((int)bounds.Width), ((int)bounds.Height), 96, 96, PixelFormats.Default); rtb.Render(WorkspaceElements); WorkspaceElements.RenderTransform = currentTransformGroup; try { using (var stm = System.IO.File.Create(path)) { // Encode as PNG format var pngEncoder = new PngBitmapEncoder(); pngEncoder.Frames.Add(BitmapFrame.Create(rtb)); pngEncoder.Save(stm); } } catch (Exception) { } }
internal void SaveWorkspaceAsImage(string path) { var initialized = false; var bounds = new Rect(); var dragCanvas = WpfUtilities.ChildOfType <DragCanvas>(this); var childrenCount = VisualTreeHelper.GetChildrenCount(dragCanvas); for (int index = 0; index < childrenCount; ++index) { var child = VisualTreeHelper.GetChild(dragCanvas, index); var firstChild = VisualTreeHelper.GetChild(child, 0); if ((!(firstChild is NodeView)) && (!(firstChild is NoteView)) && (!(firstChild is AnnotationView))) { continue; } var childBounds = VisualTreeHelper.GetDescendantBounds(child as Visual); childBounds.X = (double)(child as Visual).GetValue(Canvas.LeftProperty); childBounds.Y = (double)(child as Visual).GetValue(Canvas.TopProperty); if (initialized) { bounds.Union(childBounds); } else { initialized = true; bounds = childBounds; } } // Nothing found in the canvas, bail out. if (!initialized) { return; } // Add padding to the edge and make them multiples of two (pad 10px on each side). bounds.Width = 20 + ((((int)Math.Ceiling(bounds.Width)) + 1) & ~0x01); bounds.Height = 20 + ((((int)Math.Ceiling(bounds.Height)) + 1) & ~0x01); var currentTransformGroup = WorkspaceElements.RenderTransform as TransformGroup; WorkspaceElements.RenderTransform = new TranslateTransform(10.0 - bounds.X, 10.0 - bounds.Y); WorkspaceElements.UpdateLayout(); var rtb = new RenderTargetBitmap(((int)bounds.Width), ((int)bounds.Height), 96, 96, PixelFormats.Default); rtb.Render(WorkspaceElements); WorkspaceElements.RenderTransform = currentTransformGroup; try { using (var stm = System.IO.File.Create(path)) { // Encode as PNG format var pngEncoder = new PngBitmapEncoder(); pngEncoder.Frames.Add(BitmapFrame.Create(rtb)); pngEncoder.Save(stm); } } catch (Exception) { } }
public WorkspaceViewModel(WorkspaceModel model, DynamoViewModel dynamoViewModel) { this.DynamoViewModel = dynamoViewModel; Model = model; stateMachine = new StateMachine(this); var nodesColl = new CollectionContainer { Collection = Nodes }; WorkspaceElements.Add(nodesColl); var connColl = new CollectionContainer { Collection = Connectors }; WorkspaceElements.Add(connColl); var notesColl = new CollectionContainer { Collection = Notes }; WorkspaceElements.Add(notesColl); var errorsColl = new CollectionContainer { Collection = Errors }; WorkspaceElements.Add(errorsColl); var annotationsColl = new CollectionContainer { Collection = Annotations }; WorkspaceElements.Add(annotationsColl); //respond to collection changes on the model by creating new view models //currently, view models are added for notes and nodes //connector view models are added during connection Model.NodeAdded += Model_NodeAdded; Model.NodeRemoved += Model_NodeRemoved; Model.NodesCleared += Model_NodesCleared; Model.NoteAdded += Model_NoteAdded; Model.NoteRemoved += Model_NoteRemoved; Model.NotesCleared += Model_NotesCleared; Model.AnnotationAdded += Model_AnnotationAdded; Model.AnnotationRemoved += Model_AnnotationRemoved; Model.AnnotationsCleared += Model_AnnotationsCleared; Model.ConnectorAdded += Connectors_ConnectorAdded; Model.ConnectorDeleted += Connectors_ConnectorDeleted; Model.PropertyChanged += ModelPropertyChanged; Model.PopulateJSONWorkspace += Model_PopulateJSONWorkspace; DynamoSelection.Instance.Selection.CollectionChanged += RefreshViewOnSelectionChange; DynamoViewModel.CopyCommand.CanExecuteChanged += CopyPasteChanged; DynamoViewModel.PasteCommand.CanExecuteChanged += CopyPasteChanged; // sync collections foreach (NodeModel node in Model.Nodes) { Model_NodeAdded(node); } foreach (NoteModel note in Model.Notes) { Model_NoteAdded(note); } foreach (AnnotationModel annotation in Model.Annotations) { Model_AnnotationAdded(annotation); } foreach (ConnectorModel connector in Model.Connectors) { Connectors_ConnectorAdded(connector); } InCanvasSearchViewModel = new SearchViewModel(DynamoViewModel); InCanvasSearchViewModel.Visible = true; }