Creating a TextNote with a specific Type (i.e 1/10" Arial, 1/10" Monospace...)

Anonymous
1,923 Views
3 Replies
Message 1 of 4

Creating a TextNote with a specific Type (i.e 1/10" Arial, 1/10" Monospace...)

Anonymous
Not applicable

Hi,

 

I am trying to create a Text Note using TextNote.Create ();

 

But I am struggling to use anything other than the default TextNote Type Id.

I want to be able to create text using the 1/10" Monospace, Family Type that I already have loaded in. (See photos below)

Can anyone please tell me how to easily do that? I think there used to be a GetFamilyTypeIdByName function which no longer exists.

 

Capture2.PNG

Capture3.PNGCapture.PNG

 

Thank you!

0 Likes
Accepted solutions (3)
1,924 Views
3 Replies
Replies (3)
Message 2 of 4

recepagah12
Advocate
Advocate
Accepted solution

You can obtain the text note type from the document with Filtered ELement Collector and LINQ. Here is a code snippet.

TextNoteType textNoteType = new FilteredElementCollector(doc).OfClass(typeof(TextNoteType))
.Cast<TextNoteType>().Where(q => q.Name == "2.5mm Arial").First();

Then you can set the text note id in text note creation or after created.

TypeId = textNoteType.Id;

I hope this helps,

Recep.

0 Likes
Message 3 of 4

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Stephen,

 

Thank you for your query, and many thanks to Recep for his valuable help.

 

The method you mention, GetFamilyTypeIdByName, was probably implemented very similarly to Recep's suggestion.

 

FamilyType is really more suited for the family editor context.

 

In the project context, you have a very general base class, ElementType.

 

The TextNoteType class is derived from that, as is the FamilySymbol class.

 

Just from the name, I cannot infer whether GetFamilyTypeIdByName retrieved ElementType objects, FamilySymbol ones, or something else.

 

Anyway, for safety's sake, I implemented and added three new methods to The Building Coder samples for you:

 

  • GetElementTypeByName
  • GetFamilySymbolByName
  • GetTextNoteTypeByName

 

Their implementations are almost identical, except that they retrieve the first named object of the specific class, respectively.

 

Since there are more ElementType objects than TextNoteType ones in the project, the latter method is faster.

 

Also, all three methods could be be speeded up by using a (quick) parameter filter instead of the (slower than slow) LINQ post-processing accessed by the `First` method, as described in the recent discussion by The Building Coder on Slow, Slower Still and Faster Filtering:

 

https://thebuildingcoder.typepad.com/blog/2019/04/slow-slower-still-and-faster-filtering.html#2

 

You can see the methods I added in this diff to the previous version:

 

https://github.com/jeremytammik/the_building_coder_samples/compare/2020.0.145.1...2020.0.145.2

 

For the sake of completeness, here is their code as well:

  

  /// <summary>
  /// Return the first element type matching the given name.
  /// This filter could be speeded up by using a (quick)
  /// parameter filter instead of the (slower than slow)
  /// LINQ post-processing.
  /// </summary>
  public static ElementType GetElementTypeByName(
    Document doc,
    string name )
  {
    return new FilteredElementCollector( doc )
      .OfClass( typeof( ElementType ) )
      .First( q => q.Name.Equals( name ) )
        as ElementType;
  }

  /// <summary>
  /// Return the first family symbol matching the given name.
  /// Note that FamilySymbol is a subclass of ElementType,
  /// so this method is more restrictive above all faster
  /// than the previous one.
  /// </summary>
  public static ElementType GetFamilySymbolByName(
    Document doc,
    string name )
  {
    return new FilteredElementCollector( doc )
      .OfClass( typeof( FamilySymbol ) )
      .First( q => q.Name.Equals( name ) )
        as FamilySymbol;
  }

  /// <summary>
  /// Return the first text note type matching the given name.
  /// Note that TextNoteType is a subclass of ElementType,
  /// so this method is more restrictive above all faster
  /// than Util.GetElementTypeByName.
  /// </summary>
  TextNoteType GetTextNoteTypeByName( 
    Document doc, 
    string name )
  {
    return new FilteredElementCollector( doc )
      .OfClass( typeof( TextNoteType ) )
      .First( q => q.Name.Equals( name ) )
        as TextNoteType;
  }

   

I hope this helps.

 

Best regards,

 

Jeremy

 



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

Message 4 of 4

jeremytammik
Autodesk
Autodesk
Accepted solution