2022-01-19 12:18:33 +11:00
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2021 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/>.
2024-06-28 12:36:31 +10:00
import ifcopenshell . api . root
import ifcopenshell . api . project
import ifcopenshell . api . aggregate
2024-05-10 11:21:53 +05:00
import ifcopenshell . api . owner . settings
2021-04-07 00:31:16 +01:00
import ifcopenshell . util . date
2024-05-10 11:21:53 +05:00
from datetime import time
2021-04-07 00:31:16 +01:00
from datetime import datetime
2024-05-10 11:21:53 +05:00
from typing import Union , Optional
2021-04-07 00:31:16 +01:00
2024-05-06 14:35:39 +10:00
def add_work_schedule (
2024-05-10 11:21:53 +05:00
file : ifcopenshell . file ,
name : str = " Unnamed " ,
predefined_type : str = " NOTDEFINED " ,
2024-05-06 14:35:39 +10:00
object_type = None ,
2024-05-10 11:21:53 +05:00
start_time : Optional [ Union [ str , time ] ] = None ,
work_plan : Optional [ ifcopenshell . entity_instance ] = None ,
) - > ifcopenshell . entity_instance :
2024-05-06 14:35:39 +10:00
""" Add a new work schedule
2023-01-03 17:31:48 +11:00
2024-05-06 14:35:39 +10:00
A work schedule is a group of tasks, where the tasks are typically
either for maintenance or for construction scheduling.
2023-01-03 17:31:48 +11:00
2024-05-06 14:35:39 +10:00
:param name: The name of the work schedule.
:param predefined_type: The type of schedule, chosen from ACTUAL,
BASELINE, and PLANNED. Typically you would start with PLANNED, then
convert to a BASELINE when changes are made with separate schedules,
then have a parallel ACTUAL schedule.
:param start_time: The earlier start time when the schedule is relevant.
May be represented with an ISO standard string.
:param work_plan: The IfcWorkPlan the schedule will be part of. If not
provided, the schedule will not be grouped in a work plan and would
exist as a top level schedule in the project. This is not
recommended.
:return: The newly created IfcWorkSchedule
2023-01-03 17:31:48 +11:00
2024-05-06 14:35:39 +10:00
Example:
2023-01-10 10:16:28 +11:00
2024-05-06 14:35:39 +10:00
.. code:: python
2023-01-03 17:31:48 +11:00
2024-05-06 14:35:39 +10:00
# This will hold all our construction schedules
2024-06-28 12:36:31 +10:00
work_plan = ifcopenshell.api.sequence.add_work_plan(model, name= " Construction " )
2023-01-03 17:31:48 +11:00
2024-05-06 14:35:39 +10:00
# Let ' s imagine this is one of our schedules in our work plan.
2024-06-28 12:36:31 +10:00
schedule = ifcopenshell.api.sequence.add_work_schedule(model,
2024-05-06 14:35:39 +10:00
name= " Construction Schedule A " , work_plan=work_plan)
2023-01-03 17:31:48 +11:00
2024-05-06 14:35:39 +10:00
# Add a root task to represent the design milestones, and major
# project phases.
2024-06-28 12:36:31 +10:00
ifcopenshell.api.sequence.add_task(model,
2024-05-06 14:35:39 +10:00
work_schedule=schedule, name= " Milestones " , identification= " A " )
2024-06-28 12:36:31 +10:00
ifcopenshell.api.sequence.add_task(model,
2024-05-06 14:35:39 +10:00
work_schedule=schedule, name= " Design " , identification= " B " )
2024-06-28 12:36:31 +10:00
construction = ifcopenshell.api.sequence.add_task(model,
2024-05-06 14:35:39 +10:00
work_schedule=schedule, name= " Construction " , identification= " C " )
"""
2025-03-14 10:43:47 +05:00
start_time = start_time or datetime . now ( )
2024-06-28 12:36:31 +10:00
work_schedule = ifcopenshell . api . root . create_entity (
2024-05-06 14:35:39 +10:00
file ,
ifc_class = " IfcWorkSchedule " ,
2025-03-14 10:43:47 +05:00
predefined_type = predefined_type ,
name = name ,
2024-05-06 14:35:39 +10:00
)
2024-05-10 11:33:33 +05:00
if file . schema == " IFC2X3 " :
work_schedule . CreationDate = createIfcDateAndTime ( file , datetime . now ( ) )
else :
work_schedule . CreationDate = ifcopenshell . util . date . datetime2ifc ( datetime . now ( ) , " IfcDateTime " )
2024-05-06 14:35:39 +10:00
user = ifcopenshell . api . owner . settings . get_user ( file )
if user :
work_schedule . Creators = [ user . ThePerson ]
2024-05-10 11:33:33 +05:00
if file . schema == " IFC2X3 " :
2025-03-14 10:43:47 +05:00
work_schedule . StartTime = createIfcDateAndTime ( file , start_time )
2024-05-10 11:33:33 +05:00
else :
2025-03-14 10:43:47 +05:00
work_schedule . StartTime = ifcopenshell . util . date . datetime2ifc ( start_time , " IfcDateTime " )
if object_type :
work_schedule . ObjectType = object_type
if work_plan :
2024-06-28 12:36:31 +10:00
ifcopenshell . api . aggregate . assign_object (
2024-05-06 14:35:39 +10:00
file ,
2025-06-10 11:02:54 +05:00
products = [ work_schedule ] ,
relating_object = work_plan ,
2022-09-09 02:25:20 +01:00
)
2024-05-10 11:33:33 +05:00
elif file . schema != " IFC2X3 " :
2024-05-06 14:35:39 +10:00
# TODO: this is an ambiguity by buildingSMART
# See https://forums.buildingsmart.org/t/is-the-ifcworkschedule-project-declaration-mutually-exclusive-to-aggregation-within-a-relating-ifcworkplan/3510
context = file . by_type ( " IfcContext " ) [ 0 ]
2024-06-28 12:36:31 +10:00
ifcopenshell . api . project . assign_declaration (
2024-05-06 14:35:39 +10:00
file ,
definitions = [ work_schedule ] ,
relating_context = context ,
2022-09-09 02:25:20 +01:00
)
2024-05-06 14:35:39 +10:00
return work_schedule
2024-05-10 11:33:33 +05:00
def createIfcDateAndTime ( file : ifcopenshell . file , dt : datetime ) :
ifc_dt = file . create_entity ( " IfcDateAndTime " )
ifc_dt . DateComponent = file . create_entity (
" IfcCalendarDate " , * * ifcopenshell . util . date . datetime2ifc ( dt , " IfcCalendarDate " )
)
ifc_dt . TimeComponent = file . create_entity ( " IfcLocalTime " , * * ifcopenshell . util . date . datetime2ifc ( dt , " IfcLocalTime " ) )
return ifc_dt