Units system in document

Units system in document

Anonymous
Not applicable
5,586 Views
2 Replies
Message 1 of 3

Units system in document

Anonymous
Not applicable

Hi everyone,

Here is my code to create a detail line. I want to create a line with 100mm in Length. But the length of the line is not 100mm as i need.

Thank you for your help.

 


    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            UIApplication uiap = commandData.Application;
            UIDocument uidoc = uiap.ActiveUIDocument;
            Document doc = uidoc.Document;

            
            View view = doc.ActiveView;
            XYZ startPoint = new XYZ(0, 0, 0);
            XYZ endPoint = new XYZ(100, 0, 0);

            using (Transaction t = new Transaction(doc, "Creat Detail Line"))
            {
                t.Start();
                Units u = new Units(UnitSystem.Metric);
                doc.SetUnits(u);
                Line l1 = Line.CreateBound(startPoint, endPoint);
                doc.Create.NewDetailCurve(view, l1);
                t.Commit();
                TaskDialog.Show("Creat Detail Line", "Line was created");
            }

            return Result.Succeeded;
        }
    }

Ask revit api forum.png

0 Likes
Accepted solutions (1)
5,587 Views
2 Replies
Replies (2)
Message 2 of 3

jeremytammik
Autodesk
Autodesk
Accepted solution

Internally, Revit always uses imperial feet for all length related units:

 

http://thebuildingcoder.typepad.com/blog/2011/03/internal-imperial-units.html

 

You need to convert your 100 mm to feet, e.g., using constants and conversion methods like these:

 

    const double _inchToMm = 25.4;
    const double _footToMm = 12 * _inchToMm;

    /// <summary>
    /// Convert a given length in millimetres to feet.
    /// </summary>
    public static double MmToFoot( double length )
    {
      return length / _footToMm;
    }

The Building Coder provides a large collection of discussions on the topic of units:

 

http://thebuildingcoder.typepad.com/blog/units/

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 3

Mustafa.Salaheldin
Collaborator
Collaborator

As @jeremytammik indicated you need to convert the coordinates from millimeters to feet first in order to get the length correctly in mm units.

 

Please find the modified code:

 

        public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
        {
            UIApplication uiap = cmdData.Application;
            UIDocument uidoc = uiap.ActiveUIDocument;
            Document doc = uidoc.Document;


           View view = doc.ActiveView;
            XYZ startPoint = new XYZ(0, 0, 0);
            XYZ endPoint = new XYZ(UnitUtils.Convert(100, DisplayUnitType.DUT_MILLIMETERS, DisplayUnitType.DUT_DECIMAL_FEET), 0, 0);

            using (Transaction t = new Transaction(doc, "Creat Detail Line"))
            {
                t.Start();
                Line l1 = Line.CreateBound(startPoint, endPoint);
                doc.Create.NewDetailCurve(view, l1);
                t.Commit();
                TaskDialog.Show("Creat Detail Line", "Line was created");
            }

            return Result.Succeeded;
        }

If this reply satisfies your need please don't forget to mark it as an answer.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn