IfcOpenShell API now supports port connections and disconnections.

This commit is contained in:
Dion Moult
2022-02-01 16:51:42 +11:00
parent ddb38b2ea0
commit fd8aa6d142
4 changed files with 308 additions and 0 deletions
@@ -0,0 +1,115 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2022 Dion Moult <dion@thinkmoult.com>
#
# This file is part of IfcOpenShell.
#
# IfcOpenShell is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IfcOpenShell is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import test.bootstrap
import ifcopenshell.api
import ifcopenshell.util.system
class TestConnectPort(test.bootstrap.IFC4):
def test_connecting_a_port(self):
port = ifcopenshell.api.run("system.add_port", self.file)
port2 = ifcopenshell.api.run("system.add_port", self.file)
ifcopenshell.api.run("system.connect_port", self.file, port1=port, port2=port2)
assert port.ConnectedTo[0].RelatedPort == port2
assert port2.ConnectedFrom[0].RelatingPort == port
assert port2.ConnectedTo[0].RelatedPort == port
assert port.ConnectedFrom[0].RelatingPort == port2
assert port.FlowDirection == "NOTDEFINED"
assert port2.FlowDirection == "NOTDEFINED"
assert len(self.file.by_type("IfcRelConnectsPorts")) == 2
def test_not_connecting_a_port_twice(self):
port = ifcopenshell.api.run("system.add_port", self.file)
port2 = ifcopenshell.api.run("system.add_port", self.file)
ifcopenshell.api.run("system.connect_port", self.file, port1=port, port2=port2)
ifcopenshell.api.run("system.connect_port", self.file, port1=port, port2=port2)
ifcopenshell.api.run("system.connect_port", self.file, port1=port2, port2=port)
assert port.ConnectedTo[0].RelatedPort == port2
assert port2.ConnectedFrom[0].RelatingPort == port
assert port2.ConnectedTo[0].RelatedPort == port
assert port.ConnectedFrom[0].RelatingPort == port2
assert port.FlowDirection == "NOTDEFINED"
assert port2.FlowDirection == "NOTDEFINED"
assert len(self.file.by_type("IfcRelConnectsPorts")) == 2
def test_connecting_a_port_from_source_to_sink(self):
port = ifcopenshell.api.run("system.add_port", self.file)
port2 = ifcopenshell.api.run("system.add_port", self.file)
ifcopenshell.api.run("system.connect_port", self.file, port1=port, port2=port2, direction="SOURCE")
assert port.ConnectedTo[0].RelatedPort == port2
assert port2.ConnectedFrom[0].RelatingPort == port
assert port.FlowDirection == "SOURCE"
assert port2.FlowDirection == "SINK"
assert len(self.file.by_type("IfcRelConnectsPorts")) == 1
def test_connecting_a_port_from_sink_to_source(self):
port = ifcopenshell.api.run("system.add_port", self.file)
port2 = ifcopenshell.api.run("system.add_port", self.file)
ifcopenshell.api.run("system.connect_port", self.file, port1=port, port2=port2, direction="SINK")
assert port2.ConnectedTo[0].RelatedPort == port
assert port.ConnectedFrom[0].RelatingPort == port2
assert port.FlowDirection == "SINK"
assert port2.FlowDirection == "SOURCE"
assert len(self.file.by_type("IfcRelConnectsPorts")) == 1
def test_connecting_a_port_as_both_source_and_sink(self):
port = ifcopenshell.api.run("system.add_port", self.file)
port2 = ifcopenshell.api.run("system.add_port", self.file)
ifcopenshell.api.run("system.connect_port", self.file, port1=port, port2=port2, direction="SOURCEANDSINK")
assert port.ConnectedTo[0].RelatedPort == port2
assert port2.ConnectedFrom[0].RelatingPort == port
assert port2.ConnectedTo[0].RelatedPort == port
assert port.ConnectedFrom[0].RelatingPort == port2
assert port.FlowDirection == "SOURCEANDSINK"
assert port2.FlowDirection == "SOURCEANDSINK"
assert len(self.file.by_type("IfcRelConnectsPorts")) == 2
def test_ports_can_only_connect_to_one_port_at_a_time(self):
port = ifcopenshell.api.run("system.add_port", self.file)
port2 = ifcopenshell.api.run("system.add_port", self.file)
port3 = ifcopenshell.api.run("system.add_port", self.file)
ifcopenshell.api.run("system.connect_port", self.file, port1=port, port2=port3, direction="SOURCEANDSINK")
ifcopenshell.api.run("system.connect_port", self.file, port1=port, port2=port2, direction="SOURCE")
assert port.ConnectedTo[0].RelatedPort == port2
assert port2.ConnectedFrom[0].RelatingPort == port
assert port.FlowDirection == "SOURCE"
assert port2.FlowDirection == "SINK"
assert len(self.file.by_type("IfcRelConnectsPorts")) == 1
assert not port3.ConnectedTo
assert not port3.ConnectedFrom
def test_changing_a_port_from_source_and_sink_to_only_source(self):
port = ifcopenshell.api.run("system.add_port", self.file)
port2 = ifcopenshell.api.run("system.add_port", self.file)
ifcopenshell.api.run("system.connect_port", self.file, port1=port, port2=port2, direction="SOURCEANDSINK")
ifcopenshell.api.run("system.connect_port", self.file, port1=port, port2=port2, direction="SOURCE")
assert port.ConnectedTo[0].RelatedPort == port2
assert port2.ConnectedFrom[0].RelatingPort == port
assert port.FlowDirection == "SOURCE"
assert port2.FlowDirection == "SINK"
assert len(self.file.by_type("IfcRelConnectsPorts")) == 1
def test_connecting_ports_with_a_realising_element(self):
port = ifcopenshell.api.run("system.add_port", self.file)
port2 = ifcopenshell.api.run("system.add_port", self.file)
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcDuctFitting")
ifcopenshell.api.run("system.connect_port", self.file, port1=port, port2=port2, element=element)
assert self.file.by_type("IfcRelConnectsPorts")[0].RealizingElement == element
ifcopenshell.api.run("system.connect_port", self.file, port1=port, port2=port2)
assert self.file.by_type("IfcRelConnectsPorts")[0].RealizingElement is None
@@ -0,0 +1,32 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2022 Dion Moult <dion@thinkmoult.com>
#
# This file is part of IfcOpenShell.
#
# IfcOpenShell is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IfcOpenShell is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import test.bootstrap
import ifcopenshell.api
import ifcopenshell.util.system
class TestConnectPort(test.bootstrap.IFC4):
def test_disconnecting_a_port(self):
port = ifcopenshell.api.run("system.add_port", self.file)
port2 = ifcopenshell.api.run("system.add_port", self.file)
ifcopenshell.api.run("system.connect_port", self.file, port1=port, port2=port2, direction="NOTDEFINED")
ifcopenshell.api.run("system.disconnect_port", self.file, port=port)
assert port.FlowDirection == None
assert port2.FlowDirection == None
assert len(self.file.by_type("IfcRelConnectsPorts")) == 0