Python documentation and preferred way for setting/getting the Type

Python documentation and preferred way for setting/getting the Type

mmaso64U7Y
Enthusiast Enthusiast
875 Views
5 Replies
Message 1 of 6

Python documentation and preferred way for setting/getting the Type

mmaso64U7Y
Enthusiast
Enthusiast

Hi, I'm new to this forum, Revit and Revit API.  I've seen that there are equivalent ways to perform operations, thus, this situation is giving me some doubts regarding the design of code.

 

For example, when using Python, the following approaches to get the family type are equivalent:

elem = doc.GetElement(DB.ElementId(ids[0]))
ftype_id = elem.GetTypeId()
family_type = doc.GetElement(ftype_id)
family_type = elem.Symbol

 

And for setting the type,

elem.Symbol = doc.GetElement(ftype_id)
elem.ChangeTypeId(ftype_id)  # TypeError: No method matches given arguments for ChangeTypeId

 

I've been looking at https://www.revitapidocs.com/2024/7f875907-d138-999a-a8fb-83717ed07680.htm and https://www.revitapidocs.com/2023/c2c4b46d-52f4-2b1d-ce33-433fd0905d81.htm.

 

The symbol property seems to be deprecated, but it works. However, Get/ChangeTypeId seems to be the preferred way but it's not working.

 

I want to develop clean code with a stable version and long term support. Any help will be appreciated.

 

0 Likes
876 Views
5 Replies
Replies (5)
Message 2 of 6

Julian.Wandzilak
Advocate
Advocate

Regarding using symbols from @jeremytammik : 
The Symbol class has been renamed to ElementType.,the associated ElementType object to access the pa... 

I am not writing code in Python in Revit API. There are many reasons behind it but the main one is how easy it is to write it in C#. Another one is that it is statically typed language! So coming back to your question please find below a part of my code which might work as a reference: (exemplaryViewport and vp  are object of Viewport class) 

ElementType exemplaryType = doc.GetElement(exemplaryViewport.GetTypeId()) as ElementType;
(...)
using (Transaction t = new Transaction(doc, "asd asd "))
                    {
                        t.Start();
                        vp.ChangeTypeId(exemplaryType.Id);

 C# is forcing you (not very strongly, you can use var) to know what exactly you are using. In your case you are probably putting something wrong into ChangTypeId(...). 

Especially that "symbol" property is part of FamilyInstance class and not of element class. (inh: FamilyInstance : instance : element). So you might mix FamilyInstance type with other element type. In this case my element exemplaryViewport is not even having an option to use property symbol. 

 

hope it somehow helps. 

blog: w7k.pl more about me: Linkedin Profile
My add-ins for Revit: Drafter(180+ scripts) & Leveler
Message 3 of 6

mmaso64U7Y
Enthusiast
Enthusiast

Hy @Julian.Wandzilak , thanks for the reply. The full traceback of 

elem.ChangeTypeId(ftype_id)

is

mmaso64U7Y_0-1696341969078.png

I'm aware about the differences among Python, C#, c++, etc. My biggest concern is about documentation. I'm using Revit 2024.

0 Likes
Message 4 of 6

Julian.Wandzilak
Advocate
Advocate

Are you sure you are trying to set correct elementId? Are you swapping between two FamilyInstances? 

Also I found this thread on GitHub TypeError: No method matches given arguments for · Issue #2850 · DynamoDS/DynamoRevit · GitHub 

Looks like similar situation to yours so it might be a bug. I would not be surprises, it happened to me before that some things were not working in Iron Python / CPython.  But if I were you I would ask about this problem on dynamo forum, because best Revit python users are there.

 

To best of my knowledge there is no proper documentation for Revit API in Python and no one is planning to do make one. 

blog: w7k.pl more about me: Linkedin Profile
My add-ins for Revit: Drafter(180+ scripts) & Leveler
Message 5 of 6

mmaso64U7Y
Enthusiast
Enthusiast

@Julian.Wandzilak wrote:

Are you sure you are trying to set correct elementId? Are you swapping between two FamilyInstances? 


It is a TypeError. Btw, I'm trying to set the same type: elem.ChangeTypeId(elem.GetTypeId())

 

Also I found this thread on GitHub TypeError: No method matches given arguments for · Issue #2850 · DynamoDS/DynamoRevit · GitHub 

Looks like similar situation to yours so it might be a bug. I would not be surprises, it happened to me before that some things were not working in Iron Python / CPython.  But if I were you I would ask about this problem on dynamo forum, because best Revit python users are there.


Thanks, I have reported there.


To best of my knowledge there is no proper documentation for Revit API in Python and no one is planning to do make one. 


Good to know.

0 Likes
Message 6 of 6

jeremy_tammik
Alumni
Alumni

I agree with Julian: probably a simple type error, some unexpected cast taking place under the hood. The Dynamo experts can probably explain better.

   

Yes, to implement maintainable code you should definitely avoid making use of deprecated APIs. The deprecation means that they will probably disappear soon:

  

https://thebuildingcoder.typepad.com/blog/2020/08/revit-20211-sdk-and-whats-new.html#4

  

Regarding the Revit API documentation: it is a pure .NET API and hence language agnostic. The official Revit API documentation provided by the Revit API help file RevitAPI.chm in the Revit SDK does not support any specific language whatsoever. Some samples are in C# or VB.NET, but that is completely irrelevant. Please also refer to my answer to your other thread on Python documentation:

  

https://forums.autodesk.com/t5/revit-api-forum/python-documentation/td-p/12281423

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes