<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY? in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672773#M6237</link>
    <description>&lt;P&gt;Use the "Insert / Edit code sample" button in the message editor; &amp;lt;/&amp;gt; button&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How does the codeline below play into the routine? (It closes the document without saving after changes are committed?)&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;externalDoc.Close(false);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 28 Mar 2024 18:08:45 GMT</pubDate>
    <dc:creator>TripleM-Dev.net</dc:creator>
    <dc:date>2024-03-28T18:08:45Z</dc:date>
    <item>
      <title>THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672107#M6230</link>
      <description>&lt;P&gt;try&lt;BR /&gt;{&lt;BR /&gt;transaction.Start();&lt;BR /&gt;// Iterate through each drafting view in the external document&lt;BR /&gt;foreach (ViewDrafting selectedDraftingView in selectedDraftingViews)&lt;BR /&gt;{&lt;BR /&gt;if (selectedDraftingView != null)&lt;BR /&gt;{&lt;BR /&gt;// Check if the drafting view is valid&lt;BR /&gt;if (!selectedDraftingView.IsValidObject)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "The selected drafting view is invalid.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Duplicate the drafting view&lt;BR /&gt;ElementId blockId = selectedDraftingView.Duplicate(ViewDuplicateOption.Duplicate);&lt;BR /&gt;if (blockId == ElementId.InvalidElementId)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to duplicate the drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Get the duplicated block element&lt;BR /&gt;Element block = currentDoc.GetElement(blockId);&lt;BR /&gt;if (block == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to retrieve the duplicated drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Create a viewport for the duplicated drafting view&lt;BR /&gt;Viewport viewport = Viewport.Create(currentDoc, selectedSheet.Id, blockId, XYZ.Zero);&lt;BR /&gt;if (viewport == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to create viewport for the drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;transaction.Commit();&lt;BR /&gt;externalDoc.Close(false);&lt;BR /&gt;return Result.Succeeded;&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"{ex.Message}");&lt;BR /&gt;transaction.RollBack(); // Rollback transaction in case of exception&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 13:40:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672107#M6230</guid>
      <dc:creator>ed_sa</dc:creator>
      <dc:date>2024-03-28T13:40:15Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672527#M6231</link>
      <description>&lt;P&gt;All caps is considered yelling, and often classed as impolite:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="https://en.wikipedia.org/wiki/All_caps#Association_with_shouting_or_yelling" target="_blank"&gt;https://en.wikipedia.org/wiki/All_caps#Association_with_shouting_or_yelling&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 16:10:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672527#M6231</guid>
      <dc:creator>jeremy_tammik</dc:creator>
      <dc:date>2024-03-28T16:10:50Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672530#M6232</link>
      <description>&lt;P&gt;Thank you for your point of view.&amp;nbsp; Unfortunately, I haven't gotten an answer on my code as of yet.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 16:12:17 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672530#M6232</guid>
      <dc:creator>ed_sa</dc:creator>
      <dc:date>2024-03-28T16:12:17Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672533#M6233</link>
      <description>&lt;P&gt;It is attention getting though. I agree. Sorry to add another useless comment. Thanks again for your point of view. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 16:13:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672533#M6233</guid>
      <dc:creator>ed_sa</dc:creator>
      <dc:date>2024-03-28T16:13:41Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672732#M6234</link>
      <description>&lt;P&gt;ed, where is your "transaction" coming from? usually we see something like "Transaction transaction = new Transaction(doc);" before "transaction.Start()".&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 17:40:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672732#M6234</guid>
      <dc:creator>ctm_mka</dc:creator>
      <dc:date>2024-03-28T17:40:40Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672736#M6235</link>
      <description>&lt;P&gt;Right below the try statement.. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 17:42:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672736#M6235</guid>
      <dc:creator>ed_sa</dc:creator>
      <dc:date>2024-03-28T17:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672766#M6236</link>
      <description>&lt;P&gt;wait, that's in the code but you can't see it. using etc. right. yes it's there.&amp;nbsp; I get spam notice if I paste the whole code. too long prob&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 18:00:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672766#M6236</guid>
      <dc:creator>ed_sa</dc:creator>
      <dc:date>2024-03-28T18:00:53Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672773#M6237</link>
      <description>&lt;P&gt;Use the "Insert / Edit code sample" button in the message editor; &amp;lt;/&amp;gt; button&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How does the codeline below play into the routine? (It closes the document without saving after changes are committed?)&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;externalDoc.Close(false);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 18:08:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672773#M6237</guid>
      <dc:creator>TripleM-Dev.net</dc:creator>
      <dc:date>2024-03-28T18:08:45Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672812#M6238</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;using Autodesk.Revit.Attributes;&lt;BR /&gt;using Autodesk.Revit.DB;&lt;BR /&gt;using Autodesk.Revit.UI;&lt;BR /&gt;using System;&lt;BR /&gt;using System.Collections.Generic;&lt;BR /&gt;using System.Drawing;&lt;BR /&gt;using System.Drawing.Drawing2D;&lt;BR /&gt;using System.IO;&lt;BR /&gt;using System.Linq;&lt;BR /&gt;using System.Windows.Controls;&lt;BR /&gt;using System.Windows.Forms;&lt;BR /&gt;using Transaction = Autodesk.Revit.DB.Transaction;&lt;/P&gt;&lt;P&gt;namespace RevitAPI_JBA_Development_2022&lt;BR /&gt;{&lt;BR /&gt;[Transaction(TransactionMode.Manual)]&lt;BR /&gt;public class ImportDraftingViewsCommandSHORT : IExternalCommand&lt;BR /&gt;{&lt;BR /&gt;public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)&lt;BR /&gt;{&lt;BR /&gt;UIApplication uiApp = commandData.Application;&lt;BR /&gt;UIDocument uiDoc = uiApp.ActiveUIDocument;&lt;BR /&gt;Autodesk.Revit.DB.Document currentDoc = uiDoc.Document;&lt;BR /&gt;ICollection&amp;lt;ElementId&amp;gt; copiedViewIds;&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;BR /&gt;// Open a file dialog to select the external document&lt;BR /&gt;string externalDocumentPath = BrowseForFile(); // Path to your external document&lt;BR /&gt;// Implement your file dialog logic here&lt;/P&gt;&lt;P&gt;if (string.IsNullOrEmpty(externalDocumentPath))&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "No external document selected.");&lt;BR /&gt;return Result.Cancelled;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Open the external document&lt;BR /&gt;Autodesk.Revit.DB.Document externalDoc = uiApp.Application.OpenDocumentFile(externalDocumentPath);&lt;/P&gt;&lt;P&gt;if (externalDoc == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to open the external document.");&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Get all drafting views from the external document&lt;BR /&gt;//FilteredElementCollector externalViewsCollector = new FilteredElementCollector(externalDoc)&lt;BR /&gt;// .OfClass(typeof(ViewDrafting));&lt;BR /&gt;List&amp;lt;ViewDrafting&amp;gt; externalViewsCollector = GetDraftingViews(externalDoc);&lt;BR /&gt;List&amp;lt;ViewSheet&amp;gt; viewSheets = GetSheets(currentDoc);&lt;/P&gt;&lt;P&gt;EfficientSelectDraftingViewFormFinalMultipleSHORT importedStuff = new EfficientSelectDraftingViewFormFinalMultipleSHORT(externalViewsCollector, viewSheets);&lt;BR /&gt;if (importedStuff.ShowDialog() != DialogResult.OK)&lt;BR /&gt;{&lt;BR /&gt;externalDoc.Close(false);&lt;BR /&gt;return Result.Cancelled;&lt;BR /&gt;}&lt;BR /&gt;ViewSheet selectedSheet = importedStuff.SelectedSheet;&lt;BR /&gt;List&amp;lt;ViewDrafting&amp;gt; selectedDraftingViews = importedStuff.SelectedDraftingViews;&lt;BR /&gt;ICollection&amp;lt;ElementId&amp;gt; elementIdsCollection = selectedDraftingViews&lt;BR /&gt;.Select(view =&amp;gt; view.Id)&lt;BR /&gt;.Cast&amp;lt;ElementId&amp;gt;()&lt;BR /&gt;.ToList();&lt;/P&gt;&lt;P&gt;if (selectedDraftingViews != null)&lt;BR /&gt;{&lt;BR /&gt;using (Autodesk.Revit.DB.Transaction transaction = new Autodesk.Revit.DB.Transaction(currentDoc, "Transaction for all"))&lt;BR /&gt;{&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;transaction.Start();&lt;BR /&gt;// Iterate through each drafting view in the external document&lt;BR /&gt;foreach (ViewDrafting selectedDraftingView in selectedDraftingViews)&lt;BR /&gt;{&lt;BR /&gt;if (selectedDraftingView != null)&lt;BR /&gt;{&lt;BR /&gt;// Check if the drafting view is valid&lt;BR /&gt;if (!selectedDraftingView.IsValidObject)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "The selected drafting view is invalid.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Duplicate the drafting view&lt;BR /&gt;ElementId blockId = selectedDraftingView.Duplicate(ViewDuplicateOption.Duplicate);&lt;BR /&gt;if (blockId == null || blockId == ElementId.InvalidElementId)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to duplicate the drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Get the duplicated block element&lt;BR /&gt;Element block = currentDoc.GetElement(blockId);&lt;BR /&gt;if (block == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to retrieve the duplicated drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Create a viewport for the duplicated drafting view&lt;BR /&gt;Viewport viewport = Viewport.Create(currentDoc, selectedSheet.Id, blockId, XYZ.Zero);&lt;BR /&gt;if (viewport == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to create viewport for the drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;transaction.Commit();&lt;BR /&gt;externalDoc.Close(false);&lt;BR /&gt;return Result.Succeeded;&lt;BR /&gt;}&lt;BR /&gt;catch(Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"{ex.Message}");&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"{ex.Message}");&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;private string BrowseForFile()&lt;BR /&gt;{&lt;BR /&gt;using (OpenFileDialog openFileDialog = new OpenFileDialog())&lt;BR /&gt;{&lt;BR /&gt;openFileDialog.Filter = "Revit Files (*.rvt)|*.rvt|All files(*.*)|*.*";&lt;BR /&gt;openFileDialog.FilterIndex = 1;&lt;BR /&gt;openFileDialog.RestoreDirectory = true;&lt;/P&gt;&lt;P&gt;if (openFileDialog.ShowDialog() == DialogResult.OK)&lt;BR /&gt;{&lt;BR /&gt;return openFileDialog.FileName;&lt;BR /&gt;}&lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;return null;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;private List&amp;lt;ViewDrafting&amp;gt; GetDraftingViews(Autodesk.Revit.DB.Document document)&lt;BR /&gt;{&lt;BR /&gt;List&amp;lt;ViewDrafting&amp;gt; draftingViews = new FilteredElementCollector(document)&lt;BR /&gt;.OfClass(typeof(ViewDrafting))&lt;BR /&gt;.Cast&amp;lt;ViewDrafting&amp;gt;()&lt;BR /&gt;.Where(v =&amp;gt; v.IsTemplate == false)&lt;BR /&gt;.ToList();&lt;/P&gt;&lt;P&gt;return draftingViews;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;public List&amp;lt;ViewSheet&amp;gt; GetSheets(Autodesk.Revit.DB.Document doc)&lt;BR /&gt;{&lt;BR /&gt;return new FilteredElementCollector(doc)&lt;BR /&gt;.OfClass(typeof(ViewSheet))&lt;BR /&gt;.Cast&amp;lt;ViewSheet&amp;gt;()&lt;BR /&gt;.ToList();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;public class EfficientSelectDraftingViewFormFinalMultipleSHORT : System.Windows.Forms.Form&lt;BR /&gt;{&lt;BR /&gt;public System.Windows.Forms.ListBox draftingViewListBox;&lt;BR /&gt;public System.Windows.Forms.ComboBox sheetComboBox;&lt;BR /&gt;public System.Windows.Forms.Button okButton;&lt;BR /&gt;public System.Windows.Forms.Button cancelButton;&lt;BR /&gt;public ViewSheet SelectedSheet { get; private set; }&lt;BR /&gt;public List&amp;lt;ViewDrafting&amp;gt; importDraftingViewsFromExternalDoc;&lt;BR /&gt;public List&amp;lt;ViewSheet&amp;gt; sheets;&lt;BR /&gt;public List&amp;lt;ViewDrafting&amp;gt; SelectedDraftingViews { get; private set; }&lt;BR /&gt;private System.Drawing.Point lastLocation;&lt;/P&gt;&lt;P&gt;public EfficientSelectDraftingViewFormFinalMultipleSHORT(List&amp;lt;ViewDrafting&amp;gt; draftingViewsFromExternalDoc, List&amp;lt;ViewSheet&amp;gt; sheets)&lt;BR /&gt;{&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;this.importDraftingViewsFromExternalDoc = draftingViewsFromExternalDoc;&lt;BR /&gt;this.sheets = sheets;&lt;/P&gt;&lt;P&gt;InitializeComponent();&lt;/P&gt;&lt;P&gt;if (importDraftingViewsFromExternalDoc != null)&lt;BR /&gt;{&lt;BR /&gt;foreach (var draftingView in importDraftingViewsFromExternalDoc)&lt;BR /&gt;{&lt;BR /&gt;if (draftingView != null)&lt;BR /&gt;{&lt;BR /&gt;draftingViewListBox.Items.Add(draftingView.Name);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;//Set the combo box style to allow multiple selection&lt;BR /&gt;draftingViewListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;if (sheets != null)&lt;BR /&gt;{&lt;BR /&gt;foreach (var sheet in sheets)&lt;BR /&gt;{&lt;BR /&gt;sheetComboBox.Items.Add(sheet.Name);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"(twenty){ex.Message}");&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;protected override void OnPaint(PaintEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;base.OnPaint(e);&lt;/P&gt;&lt;P&gt;// Draw a circular border&lt;BR /&gt;e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;&lt;BR /&gt;e.Graphics.DrawEllipse(Pens.Black, 0, 0, this.Width - 1, this.Height - 1);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;private void InitializeComponent()&lt;BR /&gt;{&lt;BR /&gt;this.draftingViewListBox = new System.Windows.Forms.ListBox();&lt;BR /&gt;this.sheetComboBox = new System.Windows.Forms.ComboBox();&lt;BR /&gt;this.okButton = new System.Windows.Forms.Button();&lt;BR /&gt;this.cancelButton = new System.Windows.Forms.Button();&lt;/P&gt;&lt;P&gt;// Form&lt;BR /&gt;//this.FormBorderStyle = FormBorderStyle.SizableToolWindow;&lt;BR /&gt;this.ClientSize = new System.Drawing.Size(320, 320); // Set the form size to fit the circle&lt;BR /&gt;this.FormBorderStyle = FormBorderStyle.None; // Remove the border&lt;BR /&gt;this.StartPosition = FormStartPosition.CenterScreen; // Center the form on the screen&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;this.Paint += CircularBorderForm_Paint;&lt;BR /&gt;this.MouseDown += Form_MouseDown;&lt;BR /&gt;this.MouseMove += Form_MouseMove;&lt;BR /&gt;this.MouseUp += Form_MouseUp;&lt;/P&gt;&lt;P&gt;// Drafting View ListBox&lt;BR /&gt;this.draftingViewListBox.FormattingEnabled = true;&lt;BR /&gt;this.draftingViewListBox.Size = new System.Drawing.Size(200, 120); // Set size&lt;BR /&gt;this.draftingViewListBox.Location = new System.Drawing.Point(60, 50); // Position&lt;BR /&gt;this.draftingViewListBox.Name = "draftingViewListBox";&lt;BR /&gt;this.Controls.Add(this.draftingViewListBox);&lt;BR /&gt;this.draftingViewListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;&lt;BR /&gt;this.draftingViewListBox.KeyDown += listBox_KeyDown;&lt;/P&gt;&lt;P&gt;// Sheet ComboBox&lt;BR /&gt;this.sheetComboBox.FormattingEnabled = true;&lt;BR /&gt;this.sheetComboBox.Size = new System.Drawing.Size(200, 21); // Set size&lt;BR /&gt;this.sheetComboBox.Location = new System.Drawing.Point(60, 190); // Position&lt;BR /&gt;this.sheetComboBox.Name = "sheetComboBox";&lt;BR /&gt;this.Controls.Add(this.sheetComboBox);&lt;/P&gt;&lt;P&gt;// Ok Button&lt;BR /&gt;this.okButton.Location = new System.Drawing.Point(60, 230); // Position&lt;BR /&gt;this.okButton.Name = "okButton";&lt;BR /&gt;this.okButton.Size = new System.Drawing.Size(75, 23); // Set size&lt;BR /&gt;this.okButton.TabIndex = 0;&lt;BR /&gt;this.okButton.Text = "OK";&lt;BR /&gt;this.okButton.UseVisualStyleBackColor = true;&lt;BR /&gt;this.okButton.Click += new System.EventHandler(this.OkButton_Click);&lt;BR /&gt;this.Controls.Add(this.okButton);&lt;/P&gt;&lt;P&gt;// Cancel Button&lt;BR /&gt;this.cancelButton.Location = new System.Drawing.Point(185, 230); // Position&lt;BR /&gt;this.cancelButton.Name = "cancelButton";&lt;BR /&gt;this.cancelButton.Size = new System.Drawing.Size(75, 23); // Set size&lt;BR /&gt;this.cancelButton.TabIndex = 0;&lt;BR /&gt;this.cancelButton.Text = "Cancel";&lt;BR /&gt;this.cancelButton.UseVisualStyleBackColor = true;&lt;BR /&gt;this.cancelButton.Click += new System.EventHandler(this.CancelButton_Click);&lt;BR /&gt;this.Controls.Add(this.cancelButton);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void CircularBorderForm_Paint(object sender, PaintEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;// Create a circular region&lt;BR /&gt;GraphicsPath path = new GraphicsPath();&lt;BR /&gt;path.AddEllipse(0, 0, this.Width - 1, this.Height - 1);&lt;BR /&gt;Region = new Region(path);&lt;/P&gt;&lt;P&gt;// Dispose the GraphicsPath to free resources&lt;BR /&gt;path.Dispose();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void Form_MouseDown(object sender, MouseEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;lastLocation = e.Location;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void Form_MouseMove(object sender, MouseEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;if (e.Button == MouseButtons.Left)&lt;BR /&gt;{&lt;BR /&gt;this.Left += e.X - lastLocation.X;&lt;BR /&gt;this.Top += e.Y - lastLocation.Y;&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void Form_MouseUp(object sender, MouseEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;lastLocation = System.Drawing.Point.Empty;&lt;BR /&gt;}&lt;BR /&gt;private void OkButton_Click(object sender, EventArgs e)&lt;BR /&gt;{&lt;BR /&gt;SelectedDraftingViews = new List&amp;lt;ViewDrafting&amp;gt;(); // Initialize the list&lt;BR /&gt;string selectedSheetName = sheetComboBox.SelectedItem as string;&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;BR /&gt;List&amp;lt;string&amp;gt; selectedDraftingViewNames = new List&amp;lt;string&amp;gt;();&lt;/P&gt;&lt;P&gt;if (!string.IsNullOrEmpty(selectedSheetName) &amp;amp;&amp;amp; draftingViewListBox != null)&lt;BR /&gt;{&lt;BR /&gt;// Get the selected drafting views&lt;BR /&gt;foreach (var selectedItem in draftingViewListBox.SelectedItems)&lt;BR /&gt;{&lt;BR /&gt;if (selectedItem != null &amp;amp;&amp;amp; !string.IsNullOrEmpty(selectedItem.ToString()))&lt;BR /&gt;{&lt;BR /&gt;if (importDraftingViewsFromExternalDoc != null)&lt;BR /&gt;{&lt;BR /&gt;var draftingView = importDraftingViewsFromExternalDoc.FirstOrDefault(x =&amp;gt; x.Name == selectedItem.ToString());&lt;BR /&gt;if (draftingView != null)&lt;BR /&gt;{&lt;BR /&gt;SelectedDraftingViews.Add(importDraftingViewsFromExternalDoc.FirstOrDefault(x =&amp;gt; x.Name == selectedItem.ToString()));&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "importDraftingViewsFromExternalDoc is null");&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;SelectedSheet = sheets.FirstOrDefault(sheet =&amp;gt;&lt;BR /&gt;sheet.Name?.Trim().Equals(selectedSheetName?.Trim(), StringComparison.OrdinalIgnoreCase) == true);&lt;/P&gt;&lt;P&gt;if (SelectedSheet == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"Selected sheet '{selectedSheetName}' not found.");&lt;BR /&gt;return;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;this.DialogResult = DialogResult.OK;&lt;BR /&gt;this.Close();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;catch (NullReferenceException nullEx)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "A null reference exception occurred. Details: " + nullEx.Message);&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"(21){ex.Message}");&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void listBox_KeyDown(object sender, KeyEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;// Check if Ctrl+A is pressed&lt;BR /&gt;if (e.Control &amp;amp;&amp;amp; e.KeyCode == Keys.A)&lt;BR /&gt;{&lt;BR /&gt;// Select all items&lt;BR /&gt;for (int i = 0; i &amp;lt; draftingViewListBox.Items.Count; i++)&lt;BR /&gt;{&lt;BR /&gt;draftingViewListBox.SetSelected(i, true);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;else if (e.KeyCode == Keys.Escape) // Check if Escape key is pressed&lt;BR /&gt;{&lt;BR /&gt;// Deselect all items&lt;BR /&gt;draftingViewListBox.ClearSelected();&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void CancelButton_Click(object sender, EventArgs e)&lt;BR /&gt;{&lt;BR /&gt;DialogResult = DialogResult.Cancel;&lt;BR /&gt;Close();&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 18:19:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672812#M6238</guid>
      <dc:creator>ed_sa</dc:creator>
      <dc:date>2024-03-28T18:19:51Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672817#M6239</link>
      <description>&lt;P&gt;using Autodesk.Revit.Attributes;&lt;BR /&gt;using Autodesk.Revit.DB;&lt;BR /&gt;using Autodesk.Revit.UI;&lt;BR /&gt;using System;&lt;BR /&gt;using System.Collections.Generic;&lt;BR /&gt;using System.Drawing;&lt;BR /&gt;using System.Drawing.Drawing2D;&lt;BR /&gt;using System.IO;&lt;BR /&gt;using System.Linq;&lt;BR /&gt;using System.Windows.Controls;&lt;BR /&gt;using System.Windows.Forms;&lt;BR /&gt;using Transaction = Autodesk.Revit.DB.Transaction;&lt;/P&gt;&lt;P&gt;namespace RevitAPI_JBA_Development_2022&lt;BR /&gt;{&lt;BR /&gt;[Transaction(TransactionMode.Manual)]&lt;BR /&gt;public class ImportDraftingViewsCommandSHORT : IExternalCommand&lt;BR /&gt;{&lt;BR /&gt;public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)&lt;BR /&gt;{&lt;BR /&gt;UIApplication uiApp = commandData.Application;&lt;BR /&gt;UIDocument uiDoc = uiApp.ActiveUIDocument;&lt;BR /&gt;Autodesk.Revit.DB.Document currentDoc = uiDoc.Document;&lt;BR /&gt;ICollection&amp;lt;ElementId&amp;gt; copiedViewIds;&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;BR /&gt;// Open a file dialog to select the external document&lt;BR /&gt;string externalDocumentPath = BrowseForFile(); // Path to your external document&lt;BR /&gt;// Implement your file dialog logic here&lt;/P&gt;&lt;P&gt;if (string.IsNullOrEmpty(externalDocumentPath))&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "No external document selected.");&lt;BR /&gt;return Result.Cancelled;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Open the external document&lt;BR /&gt;Autodesk.Revit.DB.Document externalDoc = uiApp.Application.OpenDocumentFile(externalDocumentPath);&lt;/P&gt;&lt;P&gt;if (externalDoc == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to open the external document.");&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Get all drafting views from the external document&lt;BR /&gt;//FilteredElementCollector externalViewsCollector = new FilteredElementCollector(externalDoc)&lt;BR /&gt;// .OfClass(typeof(ViewDrafting));&lt;BR /&gt;List&amp;lt;ViewDrafting&amp;gt; externalViewsCollector = GetDraftingViews(externalDoc);&lt;BR /&gt;List&amp;lt;ViewSheet&amp;gt; viewSheets = GetSheets(currentDoc);&lt;/P&gt;&lt;P&gt;EfficientSelectDraftingViewFormFinalMultipleSHORT importedStuff = new EfficientSelectDraftingViewFormFinalMultipleSHORT(externalViewsCollector, viewSheets);&lt;BR /&gt;if (importedStuff.ShowDialog() != DialogResult.OK)&lt;BR /&gt;{&lt;BR /&gt;externalDoc.Close(false);&lt;BR /&gt;return Result.Cancelled;&lt;BR /&gt;}&lt;BR /&gt;ViewSheet selectedSheet = importedStuff.SelectedSheet;&lt;BR /&gt;List&amp;lt;ViewDrafting&amp;gt; selectedDraftingViews = importedStuff.SelectedDraftingViews;&lt;BR /&gt;ICollection&amp;lt;ElementId&amp;gt; elementIdsCollection = selectedDraftingViews&lt;BR /&gt;.Select(view =&amp;gt; view.Id)&lt;BR /&gt;.Cast&amp;lt;ElementId&amp;gt;()&lt;BR /&gt;.ToList();&lt;/P&gt;&lt;P&gt;if (selectedDraftingViews != null)&lt;BR /&gt;{&lt;BR /&gt;using (Autodesk.Revit.DB.Transaction transaction = new Autodesk.Revit.DB.Transaction(currentDoc, "Transaction for all"))&lt;BR /&gt;{&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;transaction.Start();&lt;BR /&gt;// Iterate through each drafting view in the external document&lt;BR /&gt;foreach (ViewDrafting selectedDraftingView in selectedDraftingViews)&lt;BR /&gt;{&lt;BR /&gt;if (selectedDraftingView != null)&lt;BR /&gt;{&lt;BR /&gt;// Check if the drafting view is valid&lt;BR /&gt;if (!selectedDraftingView.IsValidObject)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "The selected drafting view is invalid.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Duplicate the drafting view&lt;BR /&gt;ElementId blockId = selectedDraftingView.Duplicate(ViewDuplicateOption.Duplicate);&lt;BR /&gt;if (blockId == null || blockId == ElementId.InvalidElementId)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to duplicate the drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Get the duplicated block element&lt;BR /&gt;Element block = currentDoc.GetElement(blockId);&lt;BR /&gt;if (block == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to retrieve the duplicated drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Create a viewport for the duplicated drafting view&lt;BR /&gt;Viewport viewport = Viewport.Create(currentDoc, selectedSheet.Id, blockId, XYZ.Zero);&lt;BR /&gt;if (viewport == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to create viewport for the drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;transaction.Commit();&lt;BR /&gt;externalDoc.Close(false);&lt;BR /&gt;return Result.Succeeded;&lt;BR /&gt;}&lt;BR /&gt;catch(Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"{ex.Message}");&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"{ex.Message}");&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;private string BrowseForFile()&lt;BR /&gt;{&lt;BR /&gt;using (OpenFileDialog openFileDialog = new OpenFileDialog())&lt;BR /&gt;{&lt;BR /&gt;openFileDialog.Filter = "Revit Files (*.rvt)|*.rvt|All files(*.*)|*.*";&lt;BR /&gt;openFileDialog.FilterIndex = 1;&lt;BR /&gt;openFileDialog.RestoreDirectory = true;&lt;/P&gt;&lt;P&gt;if (openFileDialog.ShowDialog() == DialogResult.OK)&lt;BR /&gt;{&lt;BR /&gt;return openFileDialog.FileName;&lt;BR /&gt;}&lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;return null;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;private List&amp;lt;ViewDrafting&amp;gt; GetDraftingViews(Autodesk.Revit.DB.Document document)&lt;BR /&gt;{&lt;BR /&gt;List&amp;lt;ViewDrafting&amp;gt; draftingViews = new FilteredElementCollector(document)&lt;BR /&gt;.OfClass(typeof(ViewDrafting))&lt;BR /&gt;.Cast&amp;lt;ViewDrafting&amp;gt;()&lt;BR /&gt;.Where(v =&amp;gt; v.IsTemplate == false)&lt;BR /&gt;.ToList();&lt;/P&gt;&lt;P&gt;return draftingViews;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;public List&amp;lt;ViewSheet&amp;gt; GetSheets(Autodesk.Revit.DB.Document doc)&lt;BR /&gt;{&lt;BR /&gt;return new FilteredElementCollector(doc)&lt;BR /&gt;.OfClass(typeof(ViewSheet))&lt;BR /&gt;.Cast&amp;lt;ViewSheet&amp;gt;()&lt;BR /&gt;.ToList();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;public class EfficientSelectDraftingViewFormFinalMultipleSHORT : System.Windows.Forms.Form&lt;BR /&gt;{&lt;BR /&gt;public System.Windows.Forms.ListBox draftingViewListBox;&lt;BR /&gt;public System.Windows.Forms.ComboBox sheetComboBox;&lt;BR /&gt;public System.Windows.Forms.Button okButton;&lt;BR /&gt;public System.Windows.Forms.Button cancelButton;&lt;BR /&gt;public ViewSheet SelectedSheet { get; private set; }&lt;BR /&gt;public List&amp;lt;ViewDrafting&amp;gt; importDraftingViewsFromExternalDoc;&lt;BR /&gt;public List&amp;lt;ViewSheet&amp;gt; sheets;&lt;BR /&gt;public List&amp;lt;ViewDrafting&amp;gt; SelectedDraftingViews { get; private set; }&lt;BR /&gt;private System.Drawing.Point lastLocation;&lt;/P&gt;&lt;P&gt;public EfficientSelectDraftingViewFormFinalMultipleSHORT(List&amp;lt;ViewDrafting&amp;gt; draftingViewsFromExternalDoc, List&amp;lt;ViewSheet&amp;gt; sheets)&lt;BR /&gt;{&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;this.importDraftingViewsFromExternalDoc = draftingViewsFromExternalDoc;&lt;BR /&gt;this.sheets = sheets;&lt;/P&gt;&lt;P&gt;InitializeComponent();&lt;/P&gt;&lt;P&gt;if (importDraftingViewsFromExternalDoc != null)&lt;BR /&gt;{&lt;BR /&gt;foreach (var draftingView in importDraftingViewsFromExternalDoc)&lt;BR /&gt;{&lt;BR /&gt;if (draftingView != null)&lt;BR /&gt;{&lt;BR /&gt;draftingViewListBox.Items.Add(draftingView.Name);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;//Set the combo box style to allow multiple selection&lt;BR /&gt;draftingViewListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;if (sheets != null)&lt;BR /&gt;{&lt;BR /&gt;foreach (var sheet in sheets)&lt;BR /&gt;{&lt;BR /&gt;sheetComboBox.Items.Add(sheet.Name);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"(twenty){ex.Message}");&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;protected override void OnPaint(PaintEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;base.OnPaint(e);&lt;/P&gt;&lt;P&gt;// Draw a circular border&lt;BR /&gt;e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;&lt;BR /&gt;e.Graphics.DrawEllipse(Pens.Black, 0, 0, this.Width - 1, this.Height - 1);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;private void InitializeComponent()&lt;BR /&gt;{&lt;BR /&gt;this.draftingViewListBox = new System.Windows.Forms.ListBox();&lt;BR /&gt;this.sheetComboBox = new System.Windows.Forms.ComboBox();&lt;BR /&gt;this.okButton = new System.Windows.Forms.Button();&lt;BR /&gt;this.cancelButton = new System.Windows.Forms.Button();&lt;/P&gt;&lt;P&gt;// Form&lt;BR /&gt;//this.FormBorderStyle = FormBorderStyle.SizableToolWindow;&lt;BR /&gt;this.ClientSize = new System.Drawing.Size(320, 320); // Set the form size to fit the circle&lt;BR /&gt;this.FormBorderStyle = FormBorderStyle.None; // Remove the border&lt;BR /&gt;this.StartPosition = FormStartPosition.CenterScreen; // Center the form on the screen&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;this.Paint += CircularBorderForm_Paint;&lt;BR /&gt;this.MouseDown += Form_MouseDown;&lt;BR /&gt;this.MouseMove += Form_MouseMove;&lt;BR /&gt;this.MouseUp += Form_MouseUp;&lt;/P&gt;&lt;P&gt;// Drafting View ListBox&lt;BR /&gt;this.draftingViewListBox.FormattingEnabled = true;&lt;BR /&gt;this.draftingViewListBox.Size = new System.Drawing.Size(200, 120); // Set size&lt;BR /&gt;this.draftingViewListBox.Location = new System.Drawing.Point(60, 50); // Position&lt;BR /&gt;this.draftingViewListBox.Name = "draftingViewListBox";&lt;BR /&gt;this.Controls.Add(this.draftingViewListBox);&lt;BR /&gt;this.draftingViewListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;&lt;BR /&gt;this.draftingViewListBox.KeyDown += listBox_KeyDown;&lt;/P&gt;&lt;P&gt;// Sheet ComboBox&lt;BR /&gt;this.sheetComboBox.FormattingEnabled = true;&lt;BR /&gt;this.sheetComboBox.Size = new System.Drawing.Size(200, 21); // Set size&lt;BR /&gt;this.sheetComboBox.Location = new System.Drawing.Point(60, 190); // Position&lt;BR /&gt;this.sheetComboBox.Name = "sheetComboBox";&lt;BR /&gt;this.Controls.Add(this.sheetComboBox);&lt;/P&gt;&lt;P&gt;// Ok Button&lt;BR /&gt;this.okButton.Location = new System.Drawing.Point(60, 230); // Position&lt;BR /&gt;this.okButton.Name = "okButton";&lt;BR /&gt;this.okButton.Size = new System.Drawing.Size(75, 23); // Set size&lt;BR /&gt;this.okButton.TabIndex = 0;&lt;BR /&gt;this.okButton.Text = "OK";&lt;BR /&gt;this.okButton.UseVisualStyleBackColor = true;&lt;BR /&gt;this.okButton.Click += new System.EventHandler(this.OkButton_Click);&lt;BR /&gt;this.Controls.Add(this.okButton);&lt;/P&gt;&lt;P&gt;// Cancel Button&lt;BR /&gt;this.cancelButton.Location = new System.Drawing.Point(185, 230); // Position&lt;BR /&gt;this.cancelButton.Name = "cancelButton";&lt;BR /&gt;this.cancelButton.Size = new System.Drawing.Size(75, 23); // Set size&lt;BR /&gt;this.cancelButton.TabIndex = 0;&lt;BR /&gt;this.cancelButton.Text = "Cancel";&lt;BR /&gt;this.cancelButton.UseVisualStyleBackColor = true;&lt;BR /&gt;this.cancelButton.Click += new System.EventHandler(this.CancelButton_Click);&lt;BR /&gt;this.Controls.Add(this.cancelButton);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void CircularBorderForm_Paint(object sender, PaintEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;// Create a circular region&lt;BR /&gt;GraphicsPath path = new GraphicsPath();&lt;BR /&gt;path.AddEllipse(0, 0, this.Width - 1, this.Height - 1);&lt;BR /&gt;Region = new Region(path);&lt;/P&gt;&lt;P&gt;// Dispose the GraphicsPath to free resources&lt;BR /&gt;path.Dispose();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void Form_MouseDown(object sender, MouseEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;lastLocation = e.Location;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void Form_MouseMove(object sender, MouseEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;if (e.Button == MouseButtons.Left)&lt;BR /&gt;{&lt;BR /&gt;this.Left += e.X - lastLocation.X;&lt;BR /&gt;this.Top += e.Y - lastLocation.Y;&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void Form_MouseUp(object sender, MouseEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;lastLocation = System.Drawing.Point.Empty;&lt;BR /&gt;}&lt;BR /&gt;private void OkButton_Click(object sender, EventArgs e)&lt;BR /&gt;{&lt;BR /&gt;SelectedDraftingViews = new List&amp;lt;ViewDrafting&amp;gt;(); // Initialize the list&lt;BR /&gt;string selectedSheetName = sheetComboBox.SelectedItem as string;&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;BR /&gt;List&amp;lt;string&amp;gt; selectedDraftingViewNames = new List&amp;lt;string&amp;gt;();&lt;/P&gt;&lt;P&gt;if (!string.IsNullOrEmpty(selectedSheetName) &amp;amp;&amp;amp; draftingViewListBox != null)&lt;BR /&gt;{&lt;BR /&gt;// Get the selected drafting views&lt;BR /&gt;foreach (var selectedItem in draftingViewListBox.SelectedItems)&lt;BR /&gt;{&lt;BR /&gt;if (selectedItem != null &amp;amp;&amp;amp; !string.IsNullOrEmpty(selectedItem.ToString()))&lt;BR /&gt;{&lt;BR /&gt;if (importDraftingViewsFromExternalDoc != null)&lt;BR /&gt;{&lt;BR /&gt;var draftingView = importDraftingViewsFromExternalDoc.FirstOrDefault(x =&amp;gt; x.Name == selectedItem.ToString());&lt;BR /&gt;if (draftingView != null)&lt;BR /&gt;{&lt;BR /&gt;SelectedDraftingViews.Add(importDraftingViewsFromExternalDoc.FirstOrDefault(x =&amp;gt; x.Name == selectedItem.ToString()));&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "importDraftingViewsFromExternalDoc is null");&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;SelectedSheet = sheets.FirstOrDefault(sheet =&amp;gt;&lt;BR /&gt;sheet.Name?.Trim().Equals(selectedSheetName?.Trim(), StringComparison.OrdinalIgnoreCase) == true);&lt;/P&gt;&lt;P&gt;if (SelectedSheet == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"Selected sheet '{selectedSheetName}' not found.");&lt;BR /&gt;return;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;this.DialogResult = DialogResult.OK;&lt;BR /&gt;this.Close();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;catch (NullReferenceException nullEx)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "A null reference exception occurred. Details: " + nullEx.Message);&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"(21){ex.Message}");&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void listBox_KeyDown(object sender, KeyEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;// Check if Ctrl+A is pressed&lt;BR /&gt;if (e.Control &amp;amp;&amp;amp; e.KeyCode == Keys.A)&lt;BR /&gt;{&lt;BR /&gt;// Select all items&lt;BR /&gt;for (int i = 0; i &amp;lt; draftingViewListBox.Items.Count; i++)&lt;BR /&gt;{&lt;BR /&gt;draftingViewListBox.SetSelected(i, true);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;else if (e.KeyCode == Keys.Escape) // Check if Escape key is pressed&lt;BR /&gt;{&lt;BR /&gt;// Deselect all items&lt;BR /&gt;draftingViewListBox.ClearSelected();&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void CancelButton_Click(object sender, EventArgs e)&lt;BR /&gt;{&lt;BR /&gt;DialogResult = DialogResult.Cancel;&lt;BR /&gt;Close();&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 18:20:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672817#M6239</guid>
      <dc:creator>ed_sa</dc:creator>
      <dc:date>2024-03-28T18:20:22Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672819#M6240</link>
      <description>&lt;P&gt;Is this related to&amp;nbsp;&lt;A href="https://forums.autodesk.com/t5/revit-api-forum/can-anyone-tell-me-why-this-code-keeps-taskdialoging-attempt-to/td-p/12670389" target="_blank" rel="noopener"&gt;this&lt;/A&gt;&amp;nbsp; post?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looks like you have a transaction for Document A, but Duplicate a View of Document B which has no active transaction.&lt;/P&gt;&lt;P&gt;Seems a post that keeps coming back about copying a Drafting view out of Document B to A, and then placing that new drafting view in Document A on a sheet in Document A.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First test small parts of the code, first try to copy a draftview with contents from Document B to A, then the rest is fairly easy.&lt;/P&gt;&lt;P&gt;And again CopyElements a View doesn't include it's content, only the view itself.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 18:20:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672819#M6240</guid>
      <dc:creator>TripleM-Dev.net</dc:creator>
      <dc:date>2024-03-28T18:20:47Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672858#M6241</link>
      <description>&lt;P&gt;so the blockId duplicate worked with the externalDoc reference. thanks for that, but it crashes on the Viewport.Create. I use blockId as the ViewId and it says it cant copy the view using that. here is my revised code. thank you very much for your help:&amp;nbsp;&lt;/P&gt;&lt;P&gt;using Autodesk.Revit.Attributes;&lt;BR /&gt;using Autodesk.Revit.DB;&lt;BR /&gt;using Autodesk.Revit.UI;&lt;BR /&gt;using System;&lt;BR /&gt;using System.Collections.Generic;&lt;BR /&gt;using System.Drawing;&lt;BR /&gt;using System.Drawing.Drawing2D;&lt;BR /&gt;using System.IO;&lt;BR /&gt;using System.Linq;&lt;BR /&gt;using System.Windows.Controls;&lt;BR /&gt;using System.Windows.Forms;&lt;BR /&gt;using Transaction = Autodesk.Revit.DB.Transaction;&lt;/P&gt;&lt;P&gt;namespace RevitAPI_JBA_Development_2022&lt;BR /&gt;{&lt;BR /&gt;[Transaction(TransactionMode.Manual)]&lt;BR /&gt;public class ImportDraftingViewsCommandSHORT : IExternalCommand&lt;BR /&gt;{&lt;BR /&gt;public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)&lt;BR /&gt;{&lt;BR /&gt;UIApplication uiApp = commandData.Application;&lt;BR /&gt;UIDocument uiDoc = uiApp.ActiveUIDocument;&lt;BR /&gt;Autodesk.Revit.DB.Document currentDoc = uiDoc.Document;&lt;BR /&gt;ICollection&amp;lt;ElementId&amp;gt; copiedViewIds;&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;BR /&gt;// Open a file dialog to select the external document&lt;BR /&gt;string externalDocumentPath = BrowseForFile(); // Path to your external document&lt;BR /&gt;// Implement your file dialog logic here&lt;/P&gt;&lt;P&gt;if (string.IsNullOrEmpty(externalDocumentPath))&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "No external document selected.");&lt;BR /&gt;return Result.Cancelled;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Open the external document&lt;BR /&gt;Autodesk.Revit.DB.Document externalDoc = uiApp.Application.OpenDocumentFile(externalDocumentPath);&lt;/P&gt;&lt;P&gt;if (externalDoc == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to open the external document.");&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Get all drafting views from the external document&lt;BR /&gt;//FilteredElementCollector externalViewsCollector = new FilteredElementCollector(externalDoc)&lt;BR /&gt;// .OfClass(typeof(ViewDrafting));&lt;BR /&gt;List&amp;lt;ViewDrafting&amp;gt; externalViewsCollector = GetDraftingViews(externalDoc);&lt;BR /&gt;List&amp;lt;ViewSheet&amp;gt; viewSheets = GetSheets(currentDoc);&lt;/P&gt;&lt;P&gt;EfficientSelectDraftingViewFormFinalMultipleSHORT importedStuff = new EfficientSelectDraftingViewFormFinalMultipleSHORT(externalViewsCollector, viewSheets);&lt;BR /&gt;if (importedStuff.ShowDialog() != DialogResult.OK)&lt;BR /&gt;{&lt;BR /&gt;externalDoc.Close(false);&lt;BR /&gt;return Result.Cancelled;&lt;BR /&gt;}&lt;BR /&gt;ViewSheet selectedSheet = importedStuff.SelectedSheet;&lt;BR /&gt;List&amp;lt;ViewDrafting&amp;gt; selectedDraftingViews = importedStuff.SelectedDraftingViews;&lt;BR /&gt;ICollection&amp;lt;ElementId&amp;gt; elementIdsCollection = selectedDraftingViews&lt;BR /&gt;.Select(view =&amp;gt; view.Id)&lt;BR /&gt;.Cast&amp;lt;ElementId&amp;gt;()&lt;BR /&gt;.ToList();&lt;/P&gt;&lt;P&gt;if (selectedDraftingViews != null)&lt;BR /&gt;{&lt;BR /&gt;using (Autodesk.Revit.DB.Transaction transaction = new Autodesk.Revit.DB.Transaction(currentDoc, "Transaction for all"))&lt;BR /&gt;{&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;transaction.Start();&lt;BR /&gt;// Iterate through each drafting view in the external document&lt;BR /&gt;foreach (ViewDrafting selectedDraftingView in selectedDraftingViews)&lt;BR /&gt;{&lt;BR /&gt;if (selectedDraftingView != null)&lt;BR /&gt;{&lt;BR /&gt;// Check if the drafting view is valid&lt;BR /&gt;if (!selectedDraftingView.IsValidObject)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "The selected drafting view is invalid.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;ElementId blockId;&lt;BR /&gt;// Duplicate the drafting view&lt;BR /&gt;using (Transaction externalTransaction = new Transaction(externalDoc, "ExternalDoc"))&lt;BR /&gt;{&lt;BR /&gt;externalTransaction.Start();&lt;BR /&gt;blockId = selectedDraftingView.Duplicate(ViewDuplicateOption.Duplicate);&lt;BR /&gt;externalTransaction.Commit();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;if (blockId == null || blockId == ElementId.InvalidElementId)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to duplicate the drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Get the duplicated block element&lt;BR /&gt;Element block = externalDoc.GetElement(blockId);&lt;BR /&gt;if (block == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to retrieve the duplicated drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Create a viewport for the duplicated drafting view&lt;BR /&gt;Viewport viewport = Viewport.Create(currentDoc, selectedSheet.Id, blockId, XYZ.Zero);&lt;BR /&gt;if (viewport == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to create viewport for the drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;transaction.Commit();&lt;BR /&gt;externalDoc.Close(false);&lt;BR /&gt;return Result.Succeeded;&lt;BR /&gt;}&lt;BR /&gt;catch(Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"{ex.Message}");&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"{ex.Message}");&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;private string BrowseForFile()&lt;BR /&gt;{&lt;BR /&gt;using (OpenFileDialog openFileDialog = new OpenFileDialog())&lt;BR /&gt;{&lt;BR /&gt;openFileDialog.Filter = "Revit Files (*.rvt)|*.rvt|All files(*.*)|*.*";&lt;BR /&gt;openFileDialog.FilterIndex = 1;&lt;BR /&gt;openFileDialog.RestoreDirectory = true;&lt;/P&gt;&lt;P&gt;if (openFileDialog.ShowDialog() == DialogResult.OK)&lt;BR /&gt;{&lt;BR /&gt;return openFileDialog.FileName;&lt;BR /&gt;}&lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;return null;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;private List&amp;lt;ViewDrafting&amp;gt; GetDraftingViews(Autodesk.Revit.DB.Document document)&lt;BR /&gt;{&lt;BR /&gt;List&amp;lt;ViewDrafting&amp;gt; draftingViews = new FilteredElementCollector(document)&lt;BR /&gt;.OfClass(typeof(ViewDrafting))&lt;BR /&gt;.Cast&amp;lt;ViewDrafting&amp;gt;()&lt;BR /&gt;.Where(v =&amp;gt; v.IsTemplate == false)&lt;BR /&gt;.ToList();&lt;/P&gt;&lt;P&gt;return draftingViews;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;public List&amp;lt;ViewSheet&amp;gt; GetSheets(Autodesk.Revit.DB.Document doc)&lt;BR /&gt;{&lt;BR /&gt;return new FilteredElementCollector(doc)&lt;BR /&gt;.OfClass(typeof(ViewSheet))&lt;BR /&gt;.Cast&amp;lt;ViewSheet&amp;gt;()&lt;BR /&gt;.ToList();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;public class EfficientSelectDraftingViewFormFinalMultipleSHORT : System.Windows.Forms.Form&lt;BR /&gt;{&lt;BR /&gt;public System.Windows.Forms.ListBox draftingViewListBox;&lt;BR /&gt;public System.Windows.Forms.ComboBox sheetComboBox;&lt;BR /&gt;public System.Windows.Forms.Button okButton;&lt;BR /&gt;public System.Windows.Forms.Button cancelButton;&lt;BR /&gt;public ViewSheet SelectedSheet { get; private set; }&lt;BR /&gt;public List&amp;lt;ViewDrafting&amp;gt; importDraftingViewsFromExternalDoc;&lt;BR /&gt;public List&amp;lt;ViewSheet&amp;gt; sheets;&lt;BR /&gt;public List&amp;lt;ViewDrafting&amp;gt; SelectedDraftingViews { get; private set; }&lt;BR /&gt;private System.Drawing.Point lastLocation;&lt;/P&gt;&lt;P&gt;public EfficientSelectDraftingViewFormFinalMultipleSHORT(List&amp;lt;ViewDrafting&amp;gt; draftingViewsFromExternalDoc, List&amp;lt;ViewSheet&amp;gt; sheets)&lt;BR /&gt;{&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;this.importDraftingViewsFromExternalDoc = draftingViewsFromExternalDoc;&lt;BR /&gt;this.sheets = sheets;&lt;/P&gt;&lt;P&gt;InitializeComponent();&lt;/P&gt;&lt;P&gt;if (importDraftingViewsFromExternalDoc != null)&lt;BR /&gt;{&lt;BR /&gt;foreach (var draftingView in importDraftingViewsFromExternalDoc)&lt;BR /&gt;{&lt;BR /&gt;if (draftingView != null)&lt;BR /&gt;{&lt;BR /&gt;draftingViewListBox.Items.Add(draftingView.Name);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;//Set the combo box style to allow multiple selection&lt;BR /&gt;draftingViewListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;if (sheets != null)&lt;BR /&gt;{&lt;BR /&gt;foreach (var sheet in sheets)&lt;BR /&gt;{&lt;BR /&gt;sheetComboBox.Items.Add(sheet.Name);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"(twenty){ex.Message}");&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;protected override void OnPaint(PaintEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;base.OnPaint(e);&lt;/P&gt;&lt;P&gt;// Draw a circular border&lt;BR /&gt;e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;&lt;BR /&gt;e.Graphics.DrawEllipse(Pens.Black, 0, 0, this.Width - 1, this.Height - 1);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;private void InitializeComponent()&lt;BR /&gt;{&lt;BR /&gt;this.draftingViewListBox = new System.Windows.Forms.ListBox();&lt;BR /&gt;this.sheetComboBox = new System.Windows.Forms.ComboBox();&lt;BR /&gt;this.okButton = new System.Windows.Forms.Button();&lt;BR /&gt;this.cancelButton = new System.Windows.Forms.Button();&lt;/P&gt;&lt;P&gt;// Form&lt;BR /&gt;//this.FormBorderStyle = FormBorderStyle.SizableToolWindow;&lt;BR /&gt;this.ClientSize = new System.Drawing.Size(320, 320); // Set the form size to fit the circle&lt;BR /&gt;this.FormBorderStyle = FormBorderStyle.None; // Remove the border&lt;BR /&gt;this.StartPosition = FormStartPosition.CenterScreen; // Center the form on the screen&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;this.Paint += CircularBorderForm_Paint;&lt;BR /&gt;this.MouseDown += Form_MouseDown;&lt;BR /&gt;this.MouseMove += Form_MouseMove;&lt;BR /&gt;this.MouseUp += Form_MouseUp;&lt;/P&gt;&lt;P&gt;// Drafting View ListBox&lt;BR /&gt;this.draftingViewListBox.FormattingEnabled = true;&lt;BR /&gt;this.draftingViewListBox.Size = new System.Drawing.Size(200, 120); // Set size&lt;BR /&gt;this.draftingViewListBox.Location = new System.Drawing.Point(60, 50); // Position&lt;BR /&gt;this.draftingViewListBox.Name = "draftingViewListBox";&lt;BR /&gt;this.Controls.Add(this.draftingViewListBox);&lt;BR /&gt;this.draftingViewListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;&lt;BR /&gt;this.draftingViewListBox.KeyDown += listBox_KeyDown;&lt;/P&gt;&lt;P&gt;// Sheet ComboBox&lt;BR /&gt;this.sheetComboBox.FormattingEnabled = true;&lt;BR /&gt;this.sheetComboBox.Size = new System.Drawing.Size(200, 21); // Set size&lt;BR /&gt;this.sheetComboBox.Location = new System.Drawing.Point(60, 190); // Position&lt;BR /&gt;this.sheetComboBox.Name = "sheetComboBox";&lt;BR /&gt;this.Controls.Add(this.sheetComboBox);&lt;/P&gt;&lt;P&gt;// Ok Button&lt;BR /&gt;this.okButton.Location = new System.Drawing.Point(60, 230); // Position&lt;BR /&gt;this.okButton.Name = "okButton";&lt;BR /&gt;this.okButton.Size = new System.Drawing.Size(75, 23); // Set size&lt;BR /&gt;this.okButton.TabIndex = 0;&lt;BR /&gt;this.okButton.Text = "OK";&lt;BR /&gt;this.okButton.UseVisualStyleBackColor = true;&lt;BR /&gt;this.okButton.Click += new System.EventHandler(this.OkButton_Click);&lt;BR /&gt;this.Controls.Add(this.okButton);&lt;/P&gt;&lt;P&gt;// Cancel Button&lt;BR /&gt;this.cancelButton.Location = new System.Drawing.Point(185, 230); // Position&lt;BR /&gt;this.cancelButton.Name = "cancelButton";&lt;BR /&gt;this.cancelButton.Size = new System.Drawing.Size(75, 23); // Set size&lt;BR /&gt;this.cancelButton.TabIndex = 0;&lt;BR /&gt;this.cancelButton.Text = "Cancel";&lt;BR /&gt;this.cancelButton.UseVisualStyleBackColor = true;&lt;BR /&gt;this.cancelButton.Click += new System.EventHandler(this.CancelButton_Click);&lt;BR /&gt;this.Controls.Add(this.cancelButton);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void CircularBorderForm_Paint(object sender, PaintEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;// Create a circular region&lt;BR /&gt;GraphicsPath path = new GraphicsPath();&lt;BR /&gt;path.AddEllipse(0, 0, this.Width - 1, this.Height - 1);&lt;BR /&gt;Region = new Region(path);&lt;/P&gt;&lt;P&gt;// Dispose the GraphicsPath to free resources&lt;BR /&gt;path.Dispose();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void Form_MouseDown(object sender, MouseEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;lastLocation = e.Location;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void Form_MouseMove(object sender, MouseEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;if (e.Button == MouseButtons.Left)&lt;BR /&gt;{&lt;BR /&gt;this.Left += e.X - lastLocation.X;&lt;BR /&gt;this.Top += e.Y - lastLocation.Y;&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void Form_MouseUp(object sender, MouseEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;lastLocation = System.Drawing.Point.Empty;&lt;BR /&gt;}&lt;BR /&gt;private void OkButton_Click(object sender, EventArgs e)&lt;BR /&gt;{&lt;BR /&gt;SelectedDraftingViews = new List&amp;lt;ViewDrafting&amp;gt;(); // Initialize the list&lt;BR /&gt;string selectedSheetName = sheetComboBox.SelectedItem as string;&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;BR /&gt;List&amp;lt;string&amp;gt; selectedDraftingViewNames = new List&amp;lt;string&amp;gt;();&lt;/P&gt;&lt;P&gt;if (!string.IsNullOrEmpty(selectedSheetName) &amp;amp;&amp;amp; draftingViewListBox != null)&lt;BR /&gt;{&lt;BR /&gt;// Get the selected drafting views&lt;BR /&gt;foreach (var selectedItem in draftingViewListBox.SelectedItems)&lt;BR /&gt;{&lt;BR /&gt;if (selectedItem != null &amp;amp;&amp;amp; !string.IsNullOrEmpty(selectedItem.ToString()))&lt;BR /&gt;{&lt;BR /&gt;if (importDraftingViewsFromExternalDoc != null)&lt;BR /&gt;{&lt;BR /&gt;var draftingView = importDraftingViewsFromExternalDoc.FirstOrDefault(x =&amp;gt; x.Name == selectedItem.ToString());&lt;BR /&gt;if (draftingView != null)&lt;BR /&gt;{&lt;BR /&gt;SelectedDraftingViews.Add(importDraftingViewsFromExternalDoc.FirstOrDefault(x =&amp;gt; x.Name == selectedItem.ToString()));&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "importDraftingViewsFromExternalDoc is null");&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;SelectedSheet = sheets.FirstOrDefault(sheet =&amp;gt;&lt;BR /&gt;sheet.Name?.Trim().Equals(selectedSheetName?.Trim(), StringComparison.OrdinalIgnoreCase) == true);&lt;/P&gt;&lt;P&gt;if (SelectedSheet == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"Selected sheet '{selectedSheetName}' not found.");&lt;BR /&gt;return;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;this.DialogResult = DialogResult.OK;&lt;BR /&gt;this.Close();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;catch (NullReferenceException nullEx)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "A null reference exception occurred. Details: " + nullEx.Message);&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"(21){ex.Message}");&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void listBox_KeyDown(object sender, KeyEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;// Check if Ctrl+A is pressed&lt;BR /&gt;if (e.Control &amp;amp;&amp;amp; e.KeyCode == Keys.A)&lt;BR /&gt;{&lt;BR /&gt;// Select all items&lt;BR /&gt;for (int i = 0; i &amp;lt; draftingViewListBox.Items.Count; i++)&lt;BR /&gt;{&lt;BR /&gt;draftingViewListBox.SetSelected(i, true);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;else if (e.KeyCode == Keys.Escape) // Check if Escape key is pressed&lt;BR /&gt;{&lt;BR /&gt;// Deselect all items&lt;BR /&gt;draftingViewListBox.ClearSelected();&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void CancelButton_Click(object sender, EventArgs e)&lt;BR /&gt;{&lt;BR /&gt;DialogResult = DialogResult.Cancel;&lt;BR /&gt;Close();&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 18:37:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672858#M6241</guid>
      <dc:creator>ed_sa</dc:creator>
      <dc:date>2024-03-28T18:37:53Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672860#M6242</link>
      <description>&lt;P&gt;so the blockId duplicate worked with the externalDoc reference. thanks for that, but it crashes on the Viewport.Create. I use blockId as the ViewId and it says it cant copy the view using that. here is my revised code. thank you very much for your help:&amp;nbsp;&lt;/P&gt;&lt;P&gt;using Autodesk.Revit.Attributes;&lt;BR /&gt;using Autodesk.Revit.DB;&lt;BR /&gt;using Autodesk.Revit.UI;&lt;BR /&gt;using System;&lt;BR /&gt;using System.Collections.Generic;&lt;BR /&gt;using System.Drawing;&lt;BR /&gt;using System.Drawing.Drawing2D;&lt;BR /&gt;using System.IO;&lt;BR /&gt;using System.Linq;&lt;BR /&gt;using System.Windows.Controls;&lt;BR /&gt;using System.Windows.Forms;&lt;BR /&gt;using Transaction = Autodesk.Revit.DB.Transaction;&lt;/P&gt;&lt;P&gt;namespace RevitAPI_JBA_Development_2022&lt;BR /&gt;{&lt;BR /&gt;[Transaction(TransactionMode.Manual)]&lt;BR /&gt;public class ImportDraftingViewsCommandSHORT : IExternalCommand&lt;BR /&gt;{&lt;BR /&gt;public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)&lt;BR /&gt;{&lt;BR /&gt;UIApplication uiApp = commandData.Application;&lt;BR /&gt;UIDocument uiDoc = uiApp.ActiveUIDocument;&lt;BR /&gt;Autodesk.Revit.DB.Document currentDoc = uiDoc.Document;&lt;BR /&gt;ICollection&amp;lt;ElementId&amp;gt; copiedViewIds;&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;BR /&gt;// Open a file dialog to select the external document&lt;BR /&gt;string externalDocumentPath = BrowseForFile(); // Path to your external document&lt;BR /&gt;// Implement your file dialog logic here&lt;/P&gt;&lt;P&gt;if (string.IsNullOrEmpty(externalDocumentPath))&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "No external document selected.");&lt;BR /&gt;return Result.Cancelled;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Open the external document&lt;BR /&gt;Autodesk.Revit.DB.Document externalDoc = uiApp.Application.OpenDocumentFile(externalDocumentPath);&lt;/P&gt;&lt;P&gt;if (externalDoc == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to open the external document.");&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Get all drafting views from the external document&lt;BR /&gt;//FilteredElementCollector externalViewsCollector = new FilteredElementCollector(externalDoc)&lt;BR /&gt;// .OfClass(typeof(ViewDrafting));&lt;BR /&gt;List&amp;lt;ViewDrafting&amp;gt; externalViewsCollector = GetDraftingViews(externalDoc);&lt;BR /&gt;List&amp;lt;ViewSheet&amp;gt; viewSheets = GetSheets(currentDoc);&lt;/P&gt;&lt;P&gt;EfficientSelectDraftingViewFormFinalMultipleSHORT importedStuff = new EfficientSelectDraftingViewFormFinalMultipleSHORT(externalViewsCollector, viewSheets);&lt;BR /&gt;if (importedStuff.ShowDialog() != DialogResult.OK)&lt;BR /&gt;{&lt;BR /&gt;externalDoc.Close(false);&lt;BR /&gt;return Result.Cancelled;&lt;BR /&gt;}&lt;BR /&gt;ViewSheet selectedSheet = importedStuff.SelectedSheet;&lt;BR /&gt;List&amp;lt;ViewDrafting&amp;gt; selectedDraftingViews = importedStuff.SelectedDraftingViews;&lt;BR /&gt;ICollection&amp;lt;ElementId&amp;gt; elementIdsCollection = selectedDraftingViews&lt;BR /&gt;.Select(view =&amp;gt; view.Id)&lt;BR /&gt;.Cast&amp;lt;ElementId&amp;gt;()&lt;BR /&gt;.ToList();&lt;/P&gt;&lt;P&gt;if (selectedDraftingViews != null)&lt;BR /&gt;{&lt;BR /&gt;using (Autodesk.Revit.DB.Transaction transaction = new Autodesk.Revit.DB.Transaction(currentDoc, "Transaction for all"))&lt;BR /&gt;{&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;transaction.Start();&lt;BR /&gt;// Iterate through each drafting view in the external document&lt;BR /&gt;foreach (ViewDrafting selectedDraftingView in selectedDraftingViews)&lt;BR /&gt;{&lt;BR /&gt;if (selectedDraftingView != null)&lt;BR /&gt;{&lt;BR /&gt;// Check if the drafting view is valid&lt;BR /&gt;if (!selectedDraftingView.IsValidObject)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "The selected drafting view is invalid.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;ElementId blockId;&lt;BR /&gt;// Duplicate the drafting view&lt;BR /&gt;using (Transaction externalTransaction = new Transaction(externalDoc, "ExternalDoc"))&lt;BR /&gt;{&lt;BR /&gt;externalTransaction.Start();&lt;BR /&gt;blockId = selectedDraftingView.Duplicate(ViewDuplicateOption.Duplicate);&lt;BR /&gt;externalTransaction.Commit();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;if (blockId == null || blockId == ElementId.InvalidElementId)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to duplicate the drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Get the duplicated block element&lt;BR /&gt;Element block = externalDoc.GetElement(blockId);&lt;BR /&gt;if (block == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to retrieve the duplicated drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Create a viewport for the duplicated drafting view&lt;BR /&gt;Viewport viewport = Viewport.Create(currentDoc, selectedSheet.Id, blockId, XYZ.Zero);&lt;BR /&gt;if (viewport == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "Failed to create viewport for the drafting view.");&lt;BR /&gt;transaction.RollBack();&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;transaction.Commit();&lt;BR /&gt;externalDoc.Close(false);&lt;BR /&gt;return Result.Succeeded;&lt;BR /&gt;}&lt;BR /&gt;catch(Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"{ex.Message}");&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"{ex.Message}");&lt;BR /&gt;return Result.Failed;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;private string BrowseForFile()&lt;BR /&gt;{&lt;BR /&gt;using (OpenFileDialog openFileDialog = new OpenFileDialog())&lt;BR /&gt;{&lt;BR /&gt;openFileDialog.Filter = "Revit Files (*.rvt)|*.rvt|All files(*.*)|*.*";&lt;BR /&gt;openFileDialog.FilterIndex = 1;&lt;BR /&gt;openFileDialog.RestoreDirectory = true;&lt;/P&gt;&lt;P&gt;if (openFileDialog.ShowDialog() == DialogResult.OK)&lt;BR /&gt;{&lt;BR /&gt;return openFileDialog.FileName;&lt;BR /&gt;}&lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;return null;&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;private List&amp;lt;ViewDrafting&amp;gt; GetDraftingViews(Autodesk.Revit.DB.Document document)&lt;BR /&gt;{&lt;BR /&gt;List&amp;lt;ViewDrafting&amp;gt; draftingViews = new FilteredElementCollector(document)&lt;BR /&gt;.OfClass(typeof(ViewDrafting))&lt;BR /&gt;.Cast&amp;lt;ViewDrafting&amp;gt;()&lt;BR /&gt;.Where(v =&amp;gt; v.IsTemplate == false)&lt;BR /&gt;.ToList();&lt;/P&gt;&lt;P&gt;return draftingViews;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;public List&amp;lt;ViewSheet&amp;gt; GetSheets(Autodesk.Revit.DB.Document doc)&lt;BR /&gt;{&lt;BR /&gt;return new FilteredElementCollector(doc)&lt;BR /&gt;.OfClass(typeof(ViewSheet))&lt;BR /&gt;.Cast&amp;lt;ViewSheet&amp;gt;()&lt;BR /&gt;.ToList();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;public class EfficientSelectDraftingViewFormFinalMultipleSHORT : System.Windows.Forms.Form&lt;BR /&gt;{&lt;BR /&gt;public System.Windows.Forms.ListBox draftingViewListBox;&lt;BR /&gt;public System.Windows.Forms.ComboBox sheetComboBox;&lt;BR /&gt;public System.Windows.Forms.Button okButton;&lt;BR /&gt;public System.Windows.Forms.Button cancelButton;&lt;BR /&gt;public ViewSheet SelectedSheet { get; private set; }&lt;BR /&gt;public List&amp;lt;ViewDrafting&amp;gt; importDraftingViewsFromExternalDoc;&lt;BR /&gt;public List&amp;lt;ViewSheet&amp;gt; sheets;&lt;BR /&gt;public List&amp;lt;ViewDrafting&amp;gt; SelectedDraftingViews { get; private set; }&lt;BR /&gt;private System.Drawing.Point lastLocation;&lt;/P&gt;&lt;P&gt;public EfficientSelectDraftingViewFormFinalMultipleSHORT(List&amp;lt;ViewDrafting&amp;gt; draftingViewsFromExternalDoc, List&amp;lt;ViewSheet&amp;gt; sheets)&lt;BR /&gt;{&lt;BR /&gt;try&lt;BR /&gt;{&lt;BR /&gt;this.importDraftingViewsFromExternalDoc = draftingViewsFromExternalDoc;&lt;BR /&gt;this.sheets = sheets;&lt;/P&gt;&lt;P&gt;InitializeComponent();&lt;/P&gt;&lt;P&gt;if (importDraftingViewsFromExternalDoc != null)&lt;BR /&gt;{&lt;BR /&gt;foreach (var draftingView in importDraftingViewsFromExternalDoc)&lt;BR /&gt;{&lt;BR /&gt;if (draftingView != null)&lt;BR /&gt;{&lt;BR /&gt;draftingViewListBox.Items.Add(draftingView.Name);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;//Set the combo box style to allow multiple selection&lt;BR /&gt;draftingViewListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;if (sheets != null)&lt;BR /&gt;{&lt;BR /&gt;foreach (var sheet in sheets)&lt;BR /&gt;{&lt;BR /&gt;sheetComboBox.Items.Add(sheet.Name);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"(twenty){ex.Message}");&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;protected override void OnPaint(PaintEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;base.OnPaint(e);&lt;/P&gt;&lt;P&gt;// Draw a circular border&lt;BR /&gt;e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;&lt;BR /&gt;e.Graphics.DrawEllipse(Pens.Black, 0, 0, this.Width - 1, this.Height - 1);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;private void InitializeComponent()&lt;BR /&gt;{&lt;BR /&gt;this.draftingViewListBox = new System.Windows.Forms.ListBox();&lt;BR /&gt;this.sheetComboBox = new System.Windows.Forms.ComboBox();&lt;BR /&gt;this.okButton = new System.Windows.Forms.Button();&lt;BR /&gt;this.cancelButton = new System.Windows.Forms.Button();&lt;/P&gt;&lt;P&gt;// Form&lt;BR /&gt;//this.FormBorderStyle = FormBorderStyle.SizableToolWindow;&lt;BR /&gt;this.ClientSize = new System.Drawing.Size(320, 320); // Set the form size to fit the circle&lt;BR /&gt;this.FormBorderStyle = FormBorderStyle.None; // Remove the border&lt;BR /&gt;this.StartPosition = FormStartPosition.CenterScreen; // Center the form on the screen&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;this.Paint += CircularBorderForm_Paint;&lt;BR /&gt;this.MouseDown += Form_MouseDown;&lt;BR /&gt;this.MouseMove += Form_MouseMove;&lt;BR /&gt;this.MouseUp += Form_MouseUp;&lt;/P&gt;&lt;P&gt;// Drafting View ListBox&lt;BR /&gt;this.draftingViewListBox.FormattingEnabled = true;&lt;BR /&gt;this.draftingViewListBox.Size = new System.Drawing.Size(200, 120); // Set size&lt;BR /&gt;this.draftingViewListBox.Location = new System.Drawing.Point(60, 50); // Position&lt;BR /&gt;this.draftingViewListBox.Name = "draftingViewListBox";&lt;BR /&gt;this.Controls.Add(this.draftingViewListBox);&lt;BR /&gt;this.draftingViewListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;&lt;BR /&gt;this.draftingViewListBox.KeyDown += listBox_KeyDown;&lt;/P&gt;&lt;P&gt;// Sheet ComboBox&lt;BR /&gt;this.sheetComboBox.FormattingEnabled = true;&lt;BR /&gt;this.sheetComboBox.Size = new System.Drawing.Size(200, 21); // Set size&lt;BR /&gt;this.sheetComboBox.Location = new System.Drawing.Point(60, 190); // Position&lt;BR /&gt;this.sheetComboBox.Name = "sheetComboBox";&lt;BR /&gt;this.Controls.Add(this.sheetComboBox);&lt;/P&gt;&lt;P&gt;// Ok Button&lt;BR /&gt;this.okButton.Location = new System.Drawing.Point(60, 230); // Position&lt;BR /&gt;this.okButton.Name = "okButton";&lt;BR /&gt;this.okButton.Size = new System.Drawing.Size(75, 23); // Set size&lt;BR /&gt;this.okButton.TabIndex = 0;&lt;BR /&gt;this.okButton.Text = "OK";&lt;BR /&gt;this.okButton.UseVisualStyleBackColor = true;&lt;BR /&gt;this.okButton.Click += new System.EventHandler(this.OkButton_Click);&lt;BR /&gt;this.Controls.Add(this.okButton);&lt;/P&gt;&lt;P&gt;// Cancel Button&lt;BR /&gt;this.cancelButton.Location = new System.Drawing.Point(185, 230); // Position&lt;BR /&gt;this.cancelButton.Name = "cancelButton";&lt;BR /&gt;this.cancelButton.Size = new System.Drawing.Size(75, 23); // Set size&lt;BR /&gt;this.cancelButton.TabIndex = 0;&lt;BR /&gt;this.cancelButton.Text = "Cancel";&lt;BR /&gt;this.cancelButton.UseVisualStyleBackColor = true;&lt;BR /&gt;this.cancelButton.Click += new System.EventHandler(this.CancelButton_Click);&lt;BR /&gt;this.Controls.Add(this.cancelButton);&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void CircularBorderForm_Paint(object sender, PaintEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;// Create a circular region&lt;BR /&gt;GraphicsPath path = new GraphicsPath();&lt;BR /&gt;path.AddEllipse(0, 0, this.Width - 1, this.Height - 1);&lt;BR /&gt;Region = new Region(path);&lt;/P&gt;&lt;P&gt;// Dispose the GraphicsPath to free resources&lt;BR /&gt;path.Dispose();&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void Form_MouseDown(object sender, MouseEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;lastLocation = e.Location;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void Form_MouseMove(object sender, MouseEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;if (e.Button == MouseButtons.Left)&lt;BR /&gt;{&lt;BR /&gt;this.Left += e.X - lastLocation.X;&lt;BR /&gt;this.Top += e.Y - lastLocation.Y;&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void Form_MouseUp(object sender, MouseEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;lastLocation = System.Drawing.Point.Empty;&lt;BR /&gt;}&lt;BR /&gt;private void OkButton_Click(object sender, EventArgs e)&lt;BR /&gt;{&lt;BR /&gt;SelectedDraftingViews = new List&amp;lt;ViewDrafting&amp;gt;(); // Initialize the list&lt;BR /&gt;string selectedSheetName = sheetComboBox.SelectedItem as string;&lt;/P&gt;&lt;P&gt;try&lt;BR /&gt;{&lt;BR /&gt;List&amp;lt;string&amp;gt; selectedDraftingViewNames = new List&amp;lt;string&amp;gt;();&lt;/P&gt;&lt;P&gt;if (!string.IsNullOrEmpty(selectedSheetName) &amp;amp;&amp;amp; draftingViewListBox != null)&lt;BR /&gt;{&lt;BR /&gt;// Get the selected drafting views&lt;BR /&gt;foreach (var selectedItem in draftingViewListBox.SelectedItems)&lt;BR /&gt;{&lt;BR /&gt;if (selectedItem != null &amp;amp;&amp;amp; !string.IsNullOrEmpty(selectedItem.ToString()))&lt;BR /&gt;{&lt;BR /&gt;if (importDraftingViewsFromExternalDoc != null)&lt;BR /&gt;{&lt;BR /&gt;var draftingView = importDraftingViewsFromExternalDoc.FirstOrDefault(x =&amp;gt; x.Name == selectedItem.ToString());&lt;BR /&gt;if (draftingView != null)&lt;BR /&gt;{&lt;BR /&gt;SelectedDraftingViews.Add(importDraftingViewsFromExternalDoc.FirstOrDefault(x =&amp;gt; x.Name == selectedItem.ToString()));&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "importDraftingViewsFromExternalDoc is null");&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;SelectedSheet = sheets.FirstOrDefault(sheet =&amp;gt;&lt;BR /&gt;sheet.Name?.Trim().Equals(selectedSheetName?.Trim(), StringComparison.OrdinalIgnoreCase) == true);&lt;/P&gt;&lt;P&gt;if (SelectedSheet == null)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"Selected sheet '{selectedSheetName}' not found.");&lt;BR /&gt;return;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;this.DialogResult = DialogResult.OK;&lt;BR /&gt;this.Close();&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;catch (NullReferenceException nullEx)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", "A null reference exception occurred. Details: " + nullEx.Message);&lt;BR /&gt;}&lt;BR /&gt;catch (Exception ex)&lt;BR /&gt;{&lt;BR /&gt;TaskDialog.Show("Error", $"(21){ex.Message}");&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void listBox_KeyDown(object sender, KeyEventArgs e)&lt;BR /&gt;{&lt;BR /&gt;// Check if Ctrl+A is pressed&lt;BR /&gt;if (e.Control &amp;amp;&amp;amp; e.KeyCode == Keys.A)&lt;BR /&gt;{&lt;BR /&gt;// Select all items&lt;BR /&gt;for (int i = 0; i &amp;lt; draftingViewListBox.Items.Count; i++)&lt;BR /&gt;{&lt;BR /&gt;draftingViewListBox.SetSelected(i, true);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;else if (e.KeyCode == Keys.Escape) // Check if Escape key is pressed&lt;BR /&gt;{&lt;BR /&gt;// Deselect all items&lt;BR /&gt;draftingViewListBox.ClearSelected();&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;private void CancelButton_Click(object sender, EventArgs e)&lt;BR /&gt;{&lt;BR /&gt;DialogResult = DialogResult.Cancel;&lt;BR /&gt;Close();&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 18:38:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672860#M6242</guid>
      <dc:creator>ed_sa</dc:creator>
      <dc:date>2024-03-28T18:38:13Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672881#M6243</link>
      <description>&lt;P&gt;Also there are two types of copy elements.one copies the elements from one document to antoher, the other copies the view from one view to another and viewport.create creates a reference to the elements within the imported copied view. However if you use copyelements, you would have to iterate on each element as it won't copy model elements and will throw an error.&amp;nbsp; so I decided to duplicate the whole draftview which is this code and skip copyelements altogether in hope the elements will come along.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2024 18:43:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12672881#M6243</guid>
      <dc:creator>ed_sa</dc:creator>
      <dc:date>2024-03-28T18:43:59Z</dc:date>
    </item>
    <item>
      <title>Re: THIS CODE ALWAYS THROWS 'ATTEMPT TO CHANGE MODEL OUTSIDE OF TRANSACTION' CAN ANYONE TELL ME WHY?</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12673557#M6244</link>
      <description>&lt;P&gt;One more thing.&amp;nbsp; The blockId Duplicate method finally worked based on your suggestion to add another transaction for the externalDoc. However, when inserting blockId, or better yet, the GetElement(blockId), into the Viewport.Create, it returns a null value for the Viewport.Create&amp;nbsp; result. That's the current problem I'm having from the code above. Sorry for the repeat code, but I wanted to share the whole code with the community, I believe it should be an addition to revit as a more direct method than insert from file. Thank you for your time sorry for any inconvenience.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Mar 2024 02:10:17 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/this-code-always-throws-attempt-to-change-model-outside-of/m-p/12673557#M6244</guid>
      <dc:creator>ed_sa</dc:creator>
      <dc:date>2024-03-29T02:10:17Z</dc:date>
    </item>
  </channel>
</rss>

