Dimension subelement not working

Dimension subelement not working

dreamofbaby0507
Contributor Contributor
339 Views
2 Replies
Message 1 of 3

Dimension subelement not working

dreamofbaby0507
Contributor
Contributor

[Revit API , CSharp]

Hi everyone, I hope you will help me!!

My code:

#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.Generic;
using System.Diagnostics;
using System.Linq;
#endregion

namespace Question
{

    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {


        public Document doc { get; set; }
        /// <summary>
        /// External command mainline
        /// </summary>
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                // Display WPF Form
                // _wpfWindowController.ShowForm(commandData.Application);
                doc = commandData.Application.ActiveUIDocument.Document;
                DimensionType dimensionType = new FilteredElementCollector(doc).OfClass(typeof(DimensionType)).Cast<DimensionType>().FirstOrDefault();

                Grid gridperH = doc.GetElement(new ElementId(419358)) as Grid;
                FamilyInstance ins = doc.GetElement(new ElementId(419291)) as FamilyInstance;
                XYZ loc = (ins.Location as LocationPoint).Point;
                Line line = Line.CreateBound(loc + XYZ.BasisY, loc + XYZ.BasisY + XYZ.BasisX);
                // Dimension OK
                FamilyInstance insSingle = doc.GetElement(new ElementId(422010)) as FamilyInstance;
                ReferenceArray referenceArr = new ReferenceArray();
                referenceArr.Append(GetReferenceFromGrid(gridperH));
                referenceArr.Append(GetCurveCenterFamilyInstance(insSingle));
                DimensionTest(line, referenceArr);
                // Dimension Sub Element : Error
                referenceArr = new ReferenceArray();
                referenceArr.Append(GetReferenceFromGrid(gridperH));
                referenceArr.Append(GetCurveCenterFamilyInstance(ins));
                DimensionTest(line, referenceArr);
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return Result.Failed;
            }
        }
        public void DimensionTest(Line line, ReferenceArray refArr)
        {
            using (Transaction tx = new Transaction(doc))
            {
                tx.Start(" TransactionName ");
                doc.Create.NewDimension(doc.ActiveView, line, refArr);
                tx.Commit();
            }
        }
        public Reference GetReferenceFromGrid(Grid grid)
        {
            Reference reference = null;
            Options DefaultOptions = new Options();
            DefaultOptions.ComputeReferences = true;
            DefaultOptions.IncludeNonVisibleObjects = true;
            DefaultOptions.View = doc.ActiveView;
            GeometryElement geoElem = grid.get_Geometry(DefaultOptions);
            double length = 0;
            foreach (var item in geoElem)
            {
                Curve curve = item as Curve;
                if (curve != null)
                {
                    if (curve.Length > length)
                    {
                        reference = curve.Reference;
                        length = curve.Length;
                    }
                }
            }
            return reference;
        }
        public Reference GetCurveCenterFamilyInstance(FamilyInstance faIns)
        {
            Options geomOptions = new Options();
            geomOptions.ComputeReferences = true;
            geomOptions.View = doc.ActiveView;
            geomOptions.IncludeNonVisibleObjects = true;
            GeometryElement gElement = faIns.get_Geometry(geomOptions);
            List<Curve> lstcu = GetCurvesElement(gElement, false);
            var curveOrigin = lstcu.Where(k => k.IsBound == true && (k as Line) != null &&
            (k.GetEndPoint(0).DistanceTo(new XYZ(0, 0, k.GetEndPoint(0).Z)) <= 0.0001 || k.GetEndPoint(1).DistanceTo(new XYZ(0, 0, k.GetEndPoint(1).Z)) <= 0.0001))
                .FirstOrDefault();
            return (curveOrigin.GetEndPoint(0).DistanceTo(XYZ.Zero) < curveOrigin.GetEndPoint(1).DistanceTo(XYZ.Zero)) ? curveOrigin.GetEndPointReference(0) : curveOrigin.GetEndPointReference(1);
        }
        public List<Curve> GetCurvesElement(GeometryElement geoEle, bool IsGetGeometryInstance = true)
        {
            List<Curve> lstcuves = GetCurvesGeoElement(geoEle, IsGetGeometryInstance);
            return lstcuves;
        }
        private List<Curve> GetCurvesGeoElement(GeometryElement geoEle, bool IsGetGeometryInstance = true)
        {
            List<Curve> lstRes = new List<Curve>();
            // get references
            foreach (var geo in geoEle)
            {
                GeometryInstance geoIns = geo as GeometryInstance;
                if (geoIns != null)
                {
                    GeometryElement newGeo = IsGetGeometryInstance == true ? geoIns.GetInstanceGeometry() : geoIns.GetSymbolGeometry();
                    if (newGeo != null)
                    {
                        List<Curve> lst = GetCurvesGeoElement(newGeo, IsGetGeometryInstance);
                        if (lst.Count > 0)
                        {
                            lstRes.AddRange(lst);
                        }
                    }
                }
                Curve curve = geo as Curve;
                if (curve != null)
                {
                    lstRes.Add(curve);
                }
            }
            return lstRes;
        }
    }
}

My problem is as follows:

 - (id=422101): If I dim the familyInstance object, it runs normally
- (id=419291): But since I dim familyInstance is a sub element  then it doesn't work!

 ------   family (419291) = Family (422010)

question2.png

My code + File Revit: https://drive.google.com/drive/folders/1T4-v_WcTcc-8KW3W9CYkU9f6iPdgrcUr?usp=sharing

 

[If there are examples, the better!!]

Thanks you very much!!!

0 Likes
340 Views
2 Replies
Replies (2)
Message 2 of 3

FAIR59
Advisor
Advisor

You are trying the place dimensions. Dimensions only work on visible objects. The dimension you created is in the database, but isn't shown because it refers to a non-visible line.

Set the value for [ geomOptions.IncludeNonVisibleObjects ] to false in the  GetCurveCenterFamilyInstance() method, and all should be fine. 

0 Likes
Message 3 of 3

dreamofbaby0507
Contributor
Contributor

I have done it. Thank you very much!!

0 Likes