<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Create DirecShape in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8856008#M41693</link>
    <description>&lt;P&gt;Congratulations on solving this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I repeat my question: are you sure you know what you are doing, creating a direct shape type and an instance for that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It probably makes a lot of sense if you have many instances.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you only need one single instance, I would imagine that it would be easier and more efficient not to create a type and an instance, but just create a normal direct shape element directly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jeremy&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 17 Jun 2019 18:02:10 GMT</pubDate>
    <dc:creator>jeremytammik</dc:creator>
    <dc:date>2019-06-17T18:02:10Z</dc:date>
    <item>
      <title>Create DirecShape</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855402#M41686</link>
      <description>&lt;P&gt;I tried to create DirecShape according to the sample code. I changed DirectShape.CreateElement () method to DirectShape.CreateElementInstance () method but it was null value. Hope everyone helps me. Thanks.&lt;/P&gt;&lt;PRE&gt;public void CreateSphereDirectShape(Document doc)
{
List&amp;lt;Curve&amp;gt; profile = new List&amp;lt;Curve&amp;gt;();
XYZ center = XYZ.Zero;
double radius = ConvertUnits.MilimeterToFeet(500);
XYZ profile00 = center;
XYZ profilePlus = center + new XYZ(0, radius, 0);
XYZ profileMinus = center - new XYZ(0, radius, 0);
profile.Add(Line.CreateBound(profilePlus, profileMinus));
profile.Add(Arc.Create(profileMinus, profilePlus, center + new XYZ(radius, 0, 0)));
CurveLoop curveLoop = CurveLoop.Create(profile);
SolidOptions options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId);
Frame frame = new Frame(center, XYZ.BasisX, -XYZ.BasisZ, XYZ.BasisY);
if (Frame.CanDefineRevitGeometry(frame) == true)
{
Solid sphere = GeometryCreationUtilities.CreateRevolvedGeometry(frame, new CurveLoop[] { curveLoop }, 0, 2 * Math.PI, options); 
using (Transaction t = new Transaction(doc, "Create sphere direct shape"))
{
t.Start();
DirectShapeType directShapeType = DirectShapeType.Create(doc, "Tested", new ElementId(BuiltInCategory.OST_GenericModel));
DirectShape ds = DirectShape.CreateElementInstance(doc, directShapeType.Id, directShapeType.Category.Id, Guid.NewGuid().ToString(), Transform.Identity);
ds.ApplicationId = "Application id";
ds.ApplicationDataId = "Geometry object id";
ds.SetShape(new GeometryObject[] { sphere });
t.Commit();
}
}
}&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Jun 2019 13:47:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855402#M41686</guid>
      <dc:creator>NhuTruong_BeyCons</dc:creator>
      <dc:date>2019-06-17T13:47:57Z</dc:date>
    </item>
    <item>
      <title>Re: Create DirecShape</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855530#M41687</link>
      <description>&lt;P&gt;Please read the CreateElementInstance documentation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.revitapidocs.com/2020/2a44c4e3-5056-eb56-f849-a7e9dcc478da.htm" target="_blank"&gt;https://www.revitapidocs.com/2020/2a44c4e3-5056-eb56-f849-a7e9dcc478da.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The fourth argument is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;definitionId&lt;BR /&gt;Type: System String &lt;BR /&gt;Id of the shape definition that was created earlier and stored via DirectShapeLibrary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are passing in Guid.NewGuid().ToString() for the definition id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That does not look as if it fulfils the stated requirements.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2019 14:39:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855530#M41687</guid>
      <dc:creator>jeremytammik</dc:creator>
      <dc:date>2019-06-17T14:39:27Z</dc:date>
    </item>
    <item>
      <title>Re: Create DirecShape</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855536#M41688</link>
      <description>&lt;P&gt;The Building Coder has shared numerous samples showing how to create direct shapes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.50" target="_blank"&gt;https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.50&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The easiest solution for you would be to use one of those.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately, I do not think any of them demonstrate using a direct shape library.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Therefore, it would be interesting to implement such an example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have good reasons to implement and use such a library, I will happily help with that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jeremy&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2019 14:42:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855536#M41688</guid>
      <dc:creator>jeremytammik</dc:creator>
      <dc:date>2019-06-17T14:42:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create DirecShape</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855545#M41689</link>
      <description>&lt;P&gt;Please always spell check your query carefully, especially the title.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;'DirecShape' will not be found by people searching for 'DirectShape'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2019 14:46:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855545#M41689</guid>
      <dc:creator>jeremytammik</dc:creator>
      <dc:date>2019-06-17T14:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: Create DirecShape</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855632#M41690</link>
      <description>&lt;P&gt;I have the python code but don't know how to switch to C #. Can you help me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;# Copyright(c) 2017, Dimitar Venkov
# @5devene, dimitar.ven@gmail.com
# www.badmonkeys.net

import clr
import System
import sys
from System.Collections.Generic import *

from itertools import repeat

pf_path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
sys.path.append("%s\IronPython 2.7\Lib" % pf_path)
import traceback

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application
isRvt2017 = int(app.VersionNumber) &amp;gt; 2016

clr.AddReference("RevitNodes")
import Revit

clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference("RevitAPI")
import Autodesk.Revit.DB as RVT


def tolist(obj1):
    if hasattr(obj1, "__iter__"):
        return obj1
    else:
        return [obj1]


def PadLists(lists):
    len1 = len(lists[0])
    for i in xrange(1, len(lists)):
        len2 = len(lists[i])
        if len2 == len1:
            continue
        elif len2 &amp;gt; len1:
            lists[i] = lists[i][:len1]
        else:
            lists[i].extend(repeat(lists[i][-1], len1 - len2))
    return lists


def NewDS(s1, cat1, name1):
    temp_path = System.IO.Path.GetTempPath()
    sat_path = "%s%s.sat" % (temp_path, name1)
    try:
        if factor != 1:
            s1 = s1.Scale(factor)
        sat1 = Geometry.ExportToSAT(s1, sat_path)
        satId = doc.Import(sat1, satOpt, view1)
        el1 = doc.GetElement(satId)
        geom1 = el1.get_Geometry(opt1)
        enum = geom1.GetEnumerator()
        enum.MoveNext()
        geom2 = enum.Current.GetInstanceGeometry()
        enum2 = geom2.GetEnumerator()
        enum2.MoveNext()
        s1 = enum2.Current
        doc.Delete(satId)
        System.IO.File.Delete(sat_path)

        if cat1 is None or not RVT.DirectShape.IsValidCategoryId(cat1.Id, doc):
            cat1Id = RVT.ElementId(RVT.BuiltInCategory.OST_GenericModel)
        else:
            cat1Id = cat1.Id

        if name1 in dst_enum:
            old_type = dst_enum[name1]
            old_type.SetShape([s1])
            fec1 = RVT.FilteredElementCollector(doc).OfClass(RVT.DirectShape).WhereElementIsNotElementType()
            insts = [i for i in fec1 if i.GetTypeId().Equals(old_type.Id)]
            if insts:
                ds1 = insts[0]
            else:
                dsLib.AddDefinitionType(name1, old_type.Id)
                if isRvt2017:
                    ds1 = RVT.DirectShape.CreateElementInstance(doc, old_type.Id, cat1Id, name1, tf1)
                else:
                    ds1 = RVT.DirectShape.CreateElementInstance(doc, old_type.Id, cat1Id, name1, tf1, "Dynamo",
                                                                "spring nodes")
                ds1.SetTypeId(old_type.Id)
        else:
            dsType1 = RVT.DirectShapeType.Create(doc, name1, cat1Id)
            dsType1.SetShape([s1])
            dsLib.AddDefinitionType(name1, dsType1.Id)
            dst_enum[name1] = dsType1
            if isRvt2017:
                ds1 = RVT.DirectShape.CreateElementInstance(doc, dsType1.Id, cat1Id, name1, tf1)
            else:
                ds1 = RVT.DirectShape.CreateElementInstance(doc, dsType1.Id, cat1Id, name1, tf1, "Dynamo",
                                                            "spring nodes")
            ds1.SetTypeId(dsType1.Id)

        return ueWrapper.Invoke(None, (ds1, False))
    except:
        return traceback.format_exc()


solids = tolist(IN[0])
cat = map(UnwrapElement, tolist(IN[1]))
names = tolist(IN[2])

satOpt = RVT.SATImportOptions()
satOpt.Placement = RVT.ImportPlacement.Origin
satOpt.Unit = RVT.ImportUnit.Foot
opt1 = RVT.Options()
opt1.ComputeReferences = True

acceptable_views = "ThreeD, FloorPlan, EngineeringPlan, CeilingPlan, Elevation, Section"
view_fec = RVT.FilteredElementCollector(doc).OfClass(RVT.View)
view1 = None
for v in view_fec:
    if v.ViewType.ToString() in acceptable_views and not v.IsTemplate:
        view1 = v
        break

units = doc.GetUnits().GetFormatOptions(RVT.UnitType.UT_Length).DisplayUnits
factor = RVT.UnitUtils.ConvertToInternalUnits(1, units)

dsLib = RVT.DirectShapeLibrary.GetDirectShapeLibrary(doc)
tf1 = RVT.Transform.Identity

dst_fec = RVT.FilteredElementCollector(doc).OfClass(RVT.DirectShapeType)
dst_enum = dict((RVT.Element.Name.__get__(i), i) for i in dst_fec)

ueWrapper = None
wrappers = clr.GetClrType(Revit.Elements.ElementWrapper).GetMethods()
for w in wrappers:
    if w.ToString().startswith("Revit.Elements.UnknownElement"):
        ueWrapper = w
        break

if len(solids) == len(names):
    if not len(solids) == len(cat):
        padded = PadLists((solids, cat))
        cat = padded[1]
    TransactionManager.Instance.EnsureInTransaction(doc)
    OUT = map(NewDS, solids, cat, names)
    TransactionManager.Instance.TransactionTaskDone()
else:
    OUT = "Make sure that each geometry\nobject has a unique type name."
satOpt.Dispose()
opt1.Dispose()&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Jun 2019 15:17:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855632#M41690</guid>
      <dc:creator>NhuTruong_BeyCons</dc:creator>
      <dc:date>2019-06-17T15:17:02Z</dc:date>
    </item>
    <item>
      <title>Re: Create DirecShape</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855641#M41691</link>
      <description>&lt;P&gt;Thank you for your prompt.&amp;nbsp;How to edit the title?&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2019 15:23:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855641#M41691</guid>
      <dc:creator>NhuTruong_BeyCons</dc:creator>
      <dc:date>2019-06-17T15:23:04Z</dc:date>
    </item>
    <item>
      <title>Re: Create DirecShape</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855725#M41692</link>
      <description>&lt;P&gt;Thank you. I have solved this problem as follows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;public void CreateSphereDirectShape(Document doc)
        {
            List&amp;lt;Curve&amp;gt; profile = new List&amp;lt;Curve&amp;gt;();
            XYZ center = XYZ.Zero;
            double radius = ConvertUnits.MilimeterToFeet(500);
            XYZ profile00 = center;
            XYZ profilePlus = center + new XYZ(0, radius, 0);
            XYZ profileMinus = center - new XYZ(0, radius, 0);
            profile.Add(Line.CreateBound(profilePlus, profileMinus));
            profile.Add(Arc.Create(profileMinus, profilePlus, center + new XYZ(radius, 0, 0)));
            CurveLoop curveLoop = CurveLoop.Create(profile);
            SolidOptions options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId);
            Frame frame = new Frame(center, XYZ.BasisX, -XYZ.BasisZ, XYZ.BasisY);
            if (Frame.CanDefineRevitGeometry(frame) == true)
            {
                Solid sphere = GeometryCreationUtilities.CreateRevolvedGeometry(frame, new CurveLoop[] { curveLoop }, 0, 2 * Math.PI, options);
                using (Transaction t = new Transaction(doc, "Create sphere direct shape"))
                {
                    t.Start();
                    DirectShapeLibrary directShapeLibrary = DirectShapeLibrary.GetDirectShapeLibrary(doc);
                    DirectShapeType directShapeType = DirectShapeType.Create(doc, "Tested", new ElementId(BuiltInCategory.OST_GenericModel));
                    directShapeType.SetShape(new List&amp;lt;GeometryObject&amp;gt;() { sphere });
                    directShapeLibrary.AddDefinitionType("Tested", directShapeType.Id);
                    DirectShape ds = DirectShape.CreateElementInstance(doc, directShapeType.Id, directShapeType.Category.Id, "Tested", Transform.Identity);
                    ds.SetTypeId(directShapeType.Id);
                    ds.ApplicationId = "Application id";
                    ds.ApplicationDataId = "Geometry object id";
                    ds.SetShape(new GeometryObject[] { sphere });
                    t.Commit();
                }
            }
        }&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Jun 2019 15:57:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8855725#M41692</guid>
      <dc:creator>NhuTruong_BeyCons</dc:creator>
      <dc:date>2019-06-17T15:57:44Z</dc:date>
    </item>
    <item>
      <title>Re: Create DirecShape</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8856008#M41693</link>
      <description>&lt;P&gt;Congratulations on solving this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I repeat my question: are you sure you know what you are doing, creating a direct shape type and an instance for that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It probably makes a lot of sense if you have many instances.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you only need one single instance, I would imagine that it would be easier and more efficient not to create a type and an instance, but just create a normal direct shape element directly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jeremy&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jun 2019 18:02:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/create-direcshape/m-p/8856008#M41693</guid>
      <dc:creator>jeremytammik</dc:creator>
      <dc:date>2019-06-17T18:02:10Z</dc:date>
    </item>
  </channel>
</rss>

