Ejemplo n.º 1
0
        /// <summary>
        /// This command merges the last created merge script onto the active editor.  It better match the formats since we are
        /// not checking.
        /// </summary>
        public static void OnMergeCapturedMergeScript(ICommandParameter parameter, ICompositionHost host, DocumentEditSite site)
        {
            if (string.IsNullOrEmpty(_lastMergeScript))
            {
                return;
            }
            // The root element is the root of what ever the active editor is editing.  This would be things like the BlockDiagram
            // or FrontPanel of a VI.
            var rootElement = site.ActiveDocumentEditor?.EditorInfo?.RootElement;

            if (rootElement != null)
            {
                // Always perform modifications from within a transaction
                // Here we are creating a user transaction which means it is undoable
                using (var transaction = rootElement.TransactionManager.BeginTransaction("Merge", TransactionPurpose.User))
                {
                    var mergeScript = MergeScript.FromString(_lastMergeScript, host);
                    var resolver    = new MergeScriptResolver(mergeScript, host);
                    resolver.Merge(rootElement);
                    // Don't forget to commit the transaction
                    transaction.Commit();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Command handler which adds a new member VI to the gtype that is currently being edited
        /// </summary>
        public static void OnAddNewMemberVI(ICommandParameter parameter, ICompositionHost host, DocumentEditSite site)
        {
            // Check to see that the user is currently editing a type and if not do nothing.
            var gTypeEnvoy = site?.ActiveDocument?.Envoy;
            var gType      = (GTypeDefinition)gTypeEnvoy?.ReferenceDefinition;

            if (gType == null)
            {
                return;
            }

            var               project     = host.GetSharedExportedValue <IDocumentManager>().ActiveProject;
            var               createInfo  = EnvoyCreateInfo.CreateForNew(VirtualInstrument.VIModelDefinitionType, "New Member VI.gvi");
            Envoy             createdItem = null;
            ILockedSourceFile createdFile = null;

            // Always perform modifications from within a transaction
            // Here we are creating a user transaction which means it is undoable
            using (var transaction = gType.TransactionManager.BeginTransaction("Add A VI", TransactionPurpose.User))
            {
                createdFile = project.CreateNewFile(gType.Scope, createInfo);
                createdFile.Envoy.UpdateStoragePath("Members\\New Member VI.gvi");
                createdItem = createdFile.Envoy;
                transaction.Commit();
            }

            // Lets add a terminal to the member data
            // First we query for the merge script provider from our gtype.  Merge scripts are snippets of code that can be
            // merged into diagrams / panels / ...
            // The gtype will provide a merge script that can be used to add data item (control / terminal) to a VI and
            // many other things
            string mergeScriptText = string.Empty;
            var    dataProviders   = gTypeEnvoy.QueryService <IProvideMergeScriptData>();

            foreach (var dataProvider in dataProviders)
            {
                foreach (var script in dataProvider.MergeScriptData)
                {
                    if (script.ClipboardDataFormat == VIDiagramControl.ClipboardDataFormat)
                    {
                        // Found the merge script for a VI diagram, we are done
                        mergeScriptText = script.MergeText;
                        break;
                    }
                }
            }
            // Now merge the script onto the diagram of the VI we just created
            if (!string.IsNullOrEmpty(mergeScriptText))
            {
                var vi = (VirtualInstrument)createdItem.ReferenceDefinition;
                // Always perform modifications from within a transaction
                // We are making this transaction a non user so that it cannot be undone it is just the initial state of the VI
                using (var transaction = vi.TransactionManager.BeginTransaction("Add A VI", TransactionPurpose.NonUser))
                {
                    var mergeScript = MergeScript.FromString(mergeScriptText, host);
                    var resolver    = new MergeScriptResolver(mergeScript, host);
                    resolver.Merge(vi.BlockDiagram);
                    // Don't forget to commit the transaction or it will cancel.
                    transaction.Commit();
                }
            }
            // Now edit the memeber VI that was just created, and this time let's edit starting on the diagram
            EditDocumentInfo editInfo = new EditDocumentInfo();

            editInfo.EditorType = typeof(VIDiagramControl);
            createdItem?.Edit(editInfo);

            // After editing dispose our lock in the file
            createdFile?.Dispose();
        }