ifcopenshell.file#

Module Contents#

ifcopenshell.file.basestring#
ifcopenshell.file.file_dict#
class ifcopenshell.file.Transaction(ifc_file)#
batch(self)#
commit(self)#
get_element_inverses(self, element)#
rollback(self)#
serialise_entity_instance(self, element)#
serialise_value(self, element, value)#
store_create(self, element)#
store_delete(self, element)#
store_edit(self, element, index, value)#
unbatch(self)#
unserialise_value(self, element, value)#
class ifcopenshell.file.file(f=None, schema=None, schema_version=None)#

Bases: object

Base class for containing IFC files.

Class has instance methods for filtering by element Id, Type, etc. Instantiated objects can be subscripted by Id or Guid

Example:

ifc_file = ifcopenshell.open(file_path)
products = ifc_file.by_type("IfcProduct")
print(products[0].id(), products[0].GlobalId) # 122 2XQ$n5SLP5MBLyL442paFx
print(products[0] == ifc_file[122] == ifc_file["2XQ$n5SLP5MBLyL442paFx"]) # True

Create a new blank IFC model

This IFC model does not have any entities in it yet. See the create_entity function for how to create new entities. All data is stored in memory. If you wish to write the IFC model to disk, see the write function.

Parameters
  • f – The underlying IfcOpenShell file object to be wrapped. This is an internal implementation detail and should generally be left as None by users.

  • schema (string) – Which IFC schema to use, chosen from “IFC2X3”, “IFC4”, or “IFC4X3”. These refer to the ISO approved versions of IFC. Defaults to “IFC4” if not specified, which is currently recommended for all new projects.

  • schema_version (tuple[int]) – If you want to specify an exact version of IFC that may not be an ISO approved version, use this argument instead of schema. IFC versions on technical.buildingsmart.org are described using 4 integers representing the major, minor, addendum, and corrigendum number. For example, (4, 0, 2, 1) refers to IFC4 ADD2 TC1, which is the official version approved by ISO when people refer to “IFC4”. Generally you should not use this argument unless you are testing non-ISO IFC releases.

Example:

# Create a new IFC4 model, create a wall, then save it to an IFC-SPF file.
model = ifcopenshell.file()
model.create_entity("IfcWall")
model.write("/path/to/model.ifc")

# Create a new IFC4X3 model
model = ifcopenshell.file(schema="IFC4X3")

# A poweruser testing out a particular version of IFC4X3
model = ifcopenshell.file(schema_version=(4, 3, 0, 1))
__getattr__(self, attr)#
__getitem__(self, key)#
__iter__(self)#
add(self, inst, _id=None)#

Adds an entity including any dependent entities to an IFC file.

If the entity already exists, it is not re-added.

batch(self)#

Low-level mechanism to speed up deletion of large subgraphs

begin_transaction(self)#
by_guid(self, guid)#

Return an IFC entity instance filtered by IFC GUID.

Parameters

guid (string) – GlobalId value in 22-character encoded form

Returns

An ifcopenshell.entity_instance.entity_instance

Return type

ifcopenshell.entity_instance.entity_instance

by_id(self, id)#

Return an IFC entity instance filtered by IFC ID.

Parameters

id (int) – STEP numerical identifier

Returns

An ifcopenshell.entity_instance.entity_instance

Return type

ifcopenshell.entity_instance.entity_instance

by_type(self, type, include_subtypes=True)#

Return IFC objects filtered by IFC Type and wrapped with the entity_instance class.

If an IFC type class has subclasses, all entities of those subclasses are also returned.

Parameters
  • type (string) – The case insensitive type of IFC class to return.

  • include_subtypes (bool) – Whether or not to return subtypes of the IFC class

Returns

A list of ifcopenshell.entity_instance.entity_instance objects

Return type

list

create_entity(self, type, *args, **kwargs)#

Create a new IFC entity in the file.

Parameters
  • type (string) – Case insensitive name of the IFC class

  • args – The positional arguments of the IFC class

  • kwargs – The keyword arguments of the IFC class

Returns

An entity instance

Return type

ifcopenshell.entity_instance.entity_instance

Example:

f = ifcopenshell.file()
f.create_entity("IfcPerson")
# >>> #1=IfcPerson($,$,$,$,$,$,$,$)
f.create_entity("IfcPerson", "Foobar")
# >>> #2=IfcPerson('Foobar',$,$,$,$,$,$,$)
f.create_entity("IfcPerson", Identification="Foobar")
# >>> #3=IfcPerson('Foobar',$,$,$,$,$,$,$)
discard_transaction(self)#
end_transaction(self)#
static from_pointer(v)#
static from_string(s)#
get_inverse(self, inst, allow_duplicate=False, with_attribute_indices=False)#

Return a list of entities that reference this entity

Parameters
  • inst (ifcopenshell.entity_instance.entity_instance) – The entity instance to get inverse relationships

  • allow_duplicate – Returns a list when True, set when False

  • with_attribute_indices – Returns pairs of <i, idx> where i[idx] is inst or contains inst. Requires allow_duplicate=True

Returns

A list of ifcopenshell.entity_instance.entity_instance objects

Return type

list

get_total_inverses(self, inst)#

Returns the number of entities that reference this entity

Parameters

inst (ifcopenshell.entity_instance.entity_instance) – The entity instance to get inverse relationships

Returns

The total number of references

Return type

int

redo(self)#
remove(self, inst)#

Deletes an IFC object in the file.

Attribute values in other entity instances that reference the deleted object will be set to null. In the case of a list or set of references, the reference to the deleted will be removed from the aggregate.

Parameters

inst (ifcopenshell.entity_instance.entity_instance) – The entity instance to delete

Return type

None

set_history_size(self, size)#
traverse(self, inst, max_levels=None, breadth_first=False)#

Get a list of all referenced instances for a particular instance including itself

Parameters
  • inst (ifcopenshell.entity_instance.entity_instance) – The entity instance to get all sub instances

  • max_levels (bool) – How far deep to recursively fetch sub instances. None or -1 means infinite.

  • breadth_first – Whether to use breadth-first search, the default is depth-first.

Returns

A list of ifcopenshell.entity_instance.entity_instance objects

Return type

list

unbatch(self)#

Low-level mechanism to speed up deletion of large subgraphs

undo(self)#
write(self, path: os.PathLike | str, format=None, zipped=False) None#

Write ifc model to file.

:param format: Force use of a specific format. Guessed from file name if None. Supported formats : .ifc, .ifcXML, .ifcZIP (equivalent to format=”.ifc” with zipped=True) For zipped .ifcXML use format=”.ifcXML” with zipped=True :param zipped: zip the file after it is written

Examples: >>> model.write(“path/to/model.ifc”) >>> model.write(“path/to/model.ifcXML”) >>> model.write(“path/to/model.ifcZIP”) >>> model.write(“path/to/model.ifcZIP”, format=”.ifcXML”, zipped=True) >>> model.write(“path/to/model.anyextension”, format=”.ifcXML”)