
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi everyone I am new to the Forum and to programming as well I work with dynamo to accomplish most of my required tasks and now diving into programming and C#,so kindly bear with me if my questions are so primitive.
I am trying to create a form that can import a sketch up model into my revit file for further development.
I am Stuck with the error that says " Cannot access transaction outside API context" and with other posts and forum I understand that the Revit API lives in a special thread and can be accessed via other thread..
Here is what I have done :
SKP_importer.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Skp2Rvt
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class SKP_Importer : IExternalCommand
{
public static Document doc;
public static Application uiapp;
public static UIDocument UIdoc;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var app = commandData.Application;
uiapp = app.Application;
UIdoc = app.ActiveUIDocument;
doc = UIdoc.Document;
var UIF = new Form1();
UIF.Show();
return Result.Succeeded;
}
public void AddSkp(string file)
{
using (Transaction ts = new Transaction(doc, "test"))
{
var optionSKP = new SKPImportOptions();
{
Transaction trans = new Transaction(doc, "Import SKP file");
trans.Start();
var ActiveView = doc.ActiveView;
var SKP_File_Id = doc.Import(file, optionSKP, ActiveView);
var SKP_file = doc.GetElement(SKP_File_Id);
var list = new List<ElementId>();
list.Add(SKP_File_Id);
UIdoc.Selection.SetElementIds(list);
trans.Commit();
}
}
}
}
//
//Here Iam trying to create an external command that can be called from my form button.
// the biggest problem is i am not able to add overload parameters to the methods.
public class Addfile : IExternalEventHandler
{
public void Execute(UIApplication app)
{
using (Transaction ts = new Transaction(doc, "test"))
{
var optionSKP = new SKPImportOptions();
{
Transaction trans = new Transaction(doc, "Import SKP file");
trans.Start();
var ActiveView = doc.ActiveView;
var SKP_File_Id = doc.Import(file, optionSKP, ActiveView);
var SKP_file = doc.GetElement(SKP_File_Id);
var list = new List<ElementId>();
list.Add(SKP_File_Id);
UIdoc.Selection.SetElementIds(list);
trans.Commit();
}
}
}
public string GetName()
{
throw new NotImplementedException();
}
}
}
Form1 file :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Skp2Rvt
{
public partial class Form1 : Form
{
public string FileLocation = "";
public Form1()
{
InitializeComponent();
}
private void BrowseButton_Click(object sender, EventArgs e)
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog openFileDlg = new Microsoft.Win32.OpenFileDialog();
var result = openFileDlg.ShowDialog();
if (result == true)
{
FileName.Text = openFileDlg.FileName;
}
}
private void button1_Click(object sender, EventArgs e)
{
FileLocation = FileName.Text;
var skp = new SKP_Importer();
skp.AddSkp(FileLocation);
}
}
}
Creating a external event was a smaller problem and the biggest problem is that Iam not able to add parameters to my methods in the external command.
please help me complete this.
Solved! Go to Solution.