- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
For some reason I can't get this to work. It looks like its all working just fine. I hit OK and elements get pinned, but then when I enter into another command this particular transaction gets rolled back and a new one starts thus unpinning my pinned elements.
I tried chaning my transaction mode to manual and manually starting and closing transactions but then I am getting an error that states that I cannot start a new transaction outside of the current context.
Can someone shed some light on this?
Here's my Window:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Autodesk.Revit.Attributes; using Autodesk.Revit.UI; using Autodesk.Revit.DB; namespace AutoPin { public partial class AutoPin : System.Windows.Forms.Form { Document doc; FilteredElementCollector fec; LogicalOrFilter lorFilter; public AutoPin(Document doc) { this.doc = doc; InitializeComponent(); updateFilter(); } private void updateFilter() { List<ElementFilter> filters = new List<ElementFilter>(); if (chkGrid.Checked) { filters.Add(new ElementClassFilter(typeof(Grid))); } if (chkLevels.Checked) { filters.Add(new ElementClassFilter(typeof(Level))); } if (chkLinks.Checked) { filters.Add(new ElementClassFilter(typeof(RevitLinkInstance))); } if (chkCADLinks.Checked) { filters.Add(new ElementClassFilter(typeof(ImportInstance))); } if (filters.Count == 0) { lblStatus.Text = "No pinnable elements"; btnGO.Enabled = false; return; } btnGO.Enabled = true; lorFilter = new LogicalOrFilter(filters); fec = new FilteredElementCollector(doc); fec.WherePasses(lorFilter); lblStatus.Text = string.Format("{0} pinnable elements", fec.Count()); } private void DoPin() { //using (Transaction trans = new Transaction(doc, "Pinning Elements")) //{ // trans.Start(); foreach (Element el in fec) { el.Pinned = true; } lblStatus.Text = string.Format("{0} elements pinned", fec.Count()); Refresh(); // trans.Commit(); //} } private void btnGO_Click(object sender, EventArgs e) { DoPin(); this.Close(); } private void chckGrid_CheckedChanged(object sender, EventArgs e) { updateFilter(); } private void chkLevels_CheckedChanged(object sender, EventArgs e) { updateFilter(); } private void chkLinks_CheckedChanged(object sender, EventArgs e) { updateFilter(); } private void chkCADLinks_CheckedChanged(object sender, EventArgs e) { updateFilter(); } private void checkAll_Clicked(object sender, EventArgs e) { foreach (System.Windows.Forms.Control c in this.Controls) { if (c.GetType() == typeof(CheckBox)) { ((CheckBox)c).Checked = true; } } } private void checkNone_Click(object sender, EventArgs e) { foreach (System.Windows.Forms.Control c in this.Controls) { if (c.GetType() == typeof(CheckBox)) { ((CheckBox)c).Checked = false; } } } } }
Here's my command:
using System; using System.Collections.Generic; using System.Linq; using Autodesk.Revit.DB; using Autodesk.Revit.DB.Architecture; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Selection; using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.Attributes; namespace AutoPin { [Transaction(TransactionMode.Automatic)] public class CmdPinElements : IExternalCommand { public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { // Get application and document objects UIApplication uiApp = commandData.Application; Document doc = uiApp.ActiveUIDocument.Document; try { AutoPin form = new AutoPin(doc); form.Show(); return Result.Succeeded; } // Catch any Exceptions and display them. catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return Result.Cancelled; } catch (Exception x) { message = x.Message; return Result.Failed; } } } }
Any help will be much appreciated.
Solved! Go to Solution.