Element visible in 3d but not 2d

Element visible in 3d but not 2d

ivas0016
Contributor Contributor
414 Views
2 Replies
Message 1 of 3

Element visible in 3d but not 2d

ivas0016
Contributor
Contributor

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;
            }
        }
    }
}

 

0 Likes
Accepted solutions (1)
415 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni
Accepted solution

You probably need to adapt your view settings appropriately. I suggest you do so manually in the end user interface first, to ensure that you have understood the principles and that it works correctly for your specific devices. Once you have it working in the UI, the API aspect will be easier to handle.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 3

ivas0016
Contributor
Contributor

It was the view range 🙂 

0 Likes