- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am currently developing a Revit Add-In using the Revit API and, while testing, I came up with this exception - Autodesk.Revit.Exceptions.InvalidOperationException - The transaction does not have a valid name assigned yet.
I don't have the experience to back me up here so I don't really see the issue here. I know there's something related to the Transaction command but I don't really see what is the issue.
Below is the code I have written:
CreateSheetCommand.cs:
#region Namespaces
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
#endregion
namespace RevitAddin
{
[Transaction(TransactionMode.Manual)]
public class CreateSheetCommand : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData2,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData2.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
// Get an available titleblocks - Filtered Element Collector
FilteredElementCollector colTitleBlocks = new FilteredElementCollector(doc);
colTitleBlocks.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_TitleBlocks);
// Modify document within a transaction
using (Transaction tx2 = new Transaction(doc))
{
tx2.Start();
// Check for titleblocks. If none, load family
if (colTitleBlocks != null)
{
LoadTitleBlock loadTB = new LoadTitleBlock();
loadTB.loadTBFamily(commandData2);
}
// Grab A3 metric TitleBlock
FamilySymbol sheetTB = null;
foreach (FamilySymbol e in colTitleBlocks)
{
if (e.Name.Contains("A3 metric"))
{
sheetTB = e;
}
}
// Create new View Sheet
ViewSheet viewSheet = ViewSheet.Create(doc, sheetTB.Id);
if (viewSheet == null)
{
throw new Exception("Failed to create new View Sheet.");
}
tx2.Commit();
}
return Result.Succeeded;
}
}
}
CreateViewCommand.cs:
#region Namespaces
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
#endregion
namespace RevitAddin
{
[Transaction(TransactionMode.Manual)]
class CreateViewCommand : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData1,
ref string message,
ElementSet elements)
{
UIApplication uiapp = commandData1.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
// Get available Views - Filtered Element Collector
FilteredElementCollector col3dViews = new FilteredElementCollector(doc);
col3dViews.OfClass(typeof(ViewFamilyType));
// Find 3D view type
var viewFamilyTypes = from elem in col3dViews
let type = elem as ViewFamilyType
where type.ViewFamily == ViewFamily.ThreeDimensional
select type;
// Modify document within a transaction
using (Transaction tx1 = new Transaction(doc))
{
tx1.Start();
// Create new 3D View of the model
View3D view = View3D.CreateIsometric(doc, viewFamilyTypes.First().Id);
if (view != null)
{
XYZ eye = new XYZ(-100, -100, 50);
XYZ up = new XYZ(0, 0, 1);
XYZ forward = new XYZ(0, 1, 0);
view.SetOrientation(new ViewOrientation3D(eye, up, forward));
}
tx1.Commit();
}
return Result.Succeeded;
}
}
}
App.cs:
#region Namespaces
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Media.Imaging;
using System.Drawing.Imaging;
using System.Windows.Media;
#endregion
namespace RevitAddin
{
public class App : IExternalApplication
{
public Result OnStartup(UIControlledApplication a)
{
// Name definition for tab & panels
string tabName = "R & D";
string panelName1 = "Sheet Creator";
string panelName2 = "View Creator";
//Bitmap creation
BitmapImage bt1Image = new BitmapImage(
new Uri("pack://application:,,,/RevitAddin;component/Resources/NewItem.png"));
BitmapImage bt2Image = new BitmapImage(
new Uri("pack://application:,,,/RevitAddin;component/Resources/NewItem.png"));
// Creating the tab
a.CreateRibbonTab(tabName);
// Creating the panels
var r_dPanel = a.CreateRibbonPanel(tabName, panelName1);
var converterPanel = a.CreateRibbonPanel(tabName, panelName2);
// Creating the buttons
var button1 = new PushButtonData("CreatorButton", "Create Sheet",
Assembly.GetExecutingAssembly().Location, "RevitAddin.CreateSheetCommand");
button1.ToolTip = "Creates A3 Sheet View";
button1.LongDescription = "PLACEHOLDER";
button1.LargeImage = bt1Image;
var button2 = new PushButtonData("ConverterButton", "Create 3D View",
Assembly.GetExecutingAssembly().Location, "RevitAddin.CreateViewCommand");
button2.ToolTip = "Creates 3D View";
button2.LongDescription = "PLACEHOLDER";
button2.LargeImage = bt2Image;
// Adding the buttons to the panels
var btn1 = r_dPanel.AddItem(button1) as PushButton;
var bt2 = converterPanel.AddItem(button2) as PushButton;
return Result.Succeeded;
}
public Result OnShutdown(UIControlledApplication a)
{
return Result.Succeeded;
}
}
}
RevitAddin.addin:
<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
<AddIn Type="Command">
<Text>Create sheet command for RevitAddin</Text>
<Description>PLACEHOLDER</Description>
<Assembly>RevitAddin.dll</Assembly>
<FullClassName>RevitAddin.CreateSheetCommand</FullClassName>
<ClientId>312e59ce-fc3b-4504-9183-eb55a51906fc</ClientId>
<VendorId>joaofmoco@gmail.com</VendorId>
<VendorDescription>João Moço, Slefty Intern</VendorDescription>
</AddIn>
<AddIn Type="Command">
<Text>Create View command for RevitAddin</Text>
<Description>PLACEHOLDER</Description>
<Assembly>RevitAddin.dll</Assembly>
<FullClassName>RevitAddin.CreateViewCommand</FullClassName>
<ClientId>249ed91b-4bf1-4563-93bc-49e44bca791f</ClientId>
<VendorId>joaofmoco@gmail.com</VendorId>
<VendorDescription>João Moço, Slefty Intern</VendorDescription>
</AddIn>
<AddIn Type="Application">
<Name>Application RevitAddin</Name>
<Assembly>RevitAddin.dll</Assembly>
<FullClassName>RevitAddin.App</FullClassName>
<VendorId>joaofmoco@gmail.com</VendorId>
<VendorDescription>João Moço, Slefty Intern</VendorDescription>
<ClientId>d16c5529-077c-4c31-8c11-c0339581f97d</ClientId>
</AddIn>
</RevitAddIns>
If there are dumb mistakes, let me know. Any feedback would be helpful and I appreciate the time spent looking at this 🙂
I also leave here a .zip file with the Visual Studio 2019 project
Solved! Go to Solution.