/// <summary> /// Command handler which adds a new VI to the project (under the default target) /// </summary> public static void OnAddNewVI(ICommandParameter parameter, ICompositionHost host, DocumentEditSite site) { var project = host.GetSharedExportedValue <IDocumentManager>().ActiveProject; var createInfo = EnvoyCreateInfo.CreateForNew(VirtualInstrument.VIModelDefinitionType, new QualifiedName("New 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 = project.TransactionManager.BeginTransaction("Add A VI", TransactionPurpose.User)) { createdFile = project.CreateNewFile(null, createInfo); createdItem = createdFile?.Envoy; transaction.Commit(); } // edit the newly created VI createdItem?.Edit(); // After editing dispose our lock in the file createdFile?.Dispose(); }
/// <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(); }