Message 1 of 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am trying to automate import of security devices into revit. I plan on having one device being manually inserted and then the rest will be automatically placed around that element. Then I have to edit how they appear in graphics 2D. When a new element is inserted using ElementTransformUtilis and then changed using typeChangeTransaction, the code works and you can see the element in 3D, however when I go into 2D its not there. I tried changing a lot of setting in vg, but nothing. Please help!
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.Linq;
namespace RevitCommands
{
[Transaction(TransactionMode.Manual)]
public class AddAccessControlReader : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
// Get the Revit document
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
try
{
// Select the source element to be copied
Reference sourceElementRef = uidoc.Selection.PickObject(ObjectType.Element, "Select the element to be copied");
Element sourceElement = doc.GetElement(sourceElementRef);
// Specify the distance
double distanceInM_x = 0;
double distanceConvert_x = UnitUtils.ConvertToInternalUnits(distanceInM_x, DisplayUnitType.DUT_METERS);
double distanceInM_y = 0;
double distanceConvert_y = UnitUtils.ConvertToInternalUnits(distanceInM_y, DisplayUnitType.DUT_METERS);
double distanceInM_z = 0.3;
double distanceConvert_z = UnitUtils.ConvertToInternalUnits(distanceInM_z, DisplayUnitType.DUT_METERS);
// Calculate the translation vector
XYZ translationVector = new XYZ(distanceConvert_x, distanceConvert_y, distanceConvert_z);
Element newElement = null; // Declare the variable here before starting any transactions
using (TransactionGroup tg = new TransactionGroup(doc))
{
tg.Start("Copy and Change Element Type");
using (Transaction copyTransaction = new Transaction(doc, "Copy Element"))
{
copyTransaction.Start();
// Duplicate the source element using ElementTransformUtils.CopyElement with the translation vector
ElementId newElementId = ElementTransformUtils.CopyElement(doc, sourceElement.Id, translationVector).First();
// Get the newly created element
newElement = doc.GetElement(newElementId); // Assign the value here
copyTransaction.Commit();
}
using (Transaction typeChangeTransaction = new Transaction(doc, "Change Element Type"))
{
typeChangeTransaction.Start();
// Select the desired element type
Reference desiredTypeRef = uidoc.Selection.PickObject(ObjectType.Element, "Select the desired element type");
Element desiredTypeElement = doc.GetElement(desiredTypeRef);
// Change the element type using the ChangeTypeId method
ElementId desiredTypeId = desiredTypeElement.GetTypeId();
newElement.ChangeTypeId(desiredTypeId);
typeChangeTransaction.Commit();
}
tg.Assimilate();
}
return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
}
}
Solved! Go to Solution.
Developer Advocacy and Support +