Skip to content

Commit

Permalink
chore: IfcPropertiesManager.setData adds expressID when missing
Browse files Browse the repository at this point in the history
  • Loading branch information
HoyosJuan committed Aug 17, 2024
1 parent 836bb43 commit d464fe4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
20 changes: 20 additions & 0 deletions packages/core/src/ifc/IfcPropertiesManager/example.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as WEBIFC from "web-ifc";
import * as OBC from "../..";

const components = new OBC.Components();
Expand Down Expand Up @@ -31,6 +32,25 @@ if (entityAttributes) {
await propertiesManager.setData(model, entityAttributes);
}

// Create a new random entity
const ifcTask = new WEBIFC.IFC4X3.IfcTask(
new WEBIFC.IFC4X3.IfcGloballyUniqueId(OBC.UUID.create()),
null,
null,
null,
null,
null,
null,
null,
null,
new WEBIFC.IFC4X3.IfcBoolean(false),
null,
null,
null,
);

await propertiesManager.setData(model, ifcTask);

// Export modified model
const modifiedBuffer = await propertiesManager.saveToIfc(
model,
Expand Down
36 changes: 32 additions & 4 deletions packages/core/src/ifc/IfcPropertiesManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class IfcPropertiesManager extends Component implements Disposable {
}

/**
* Method to set properties data in the model.
* Method to add or update entity attributes in the model.
*
* @param model - The FragmentsGroup model in which to set the properties.
* @param dataToSave - An array of objects representing the properties to be saved.
Expand All @@ -178,8 +178,10 @@ export class IfcPropertiesManager extends Component implements Disposable {
*/
async setData(model: FragmentsGroup, ...dataToSave: Record<string, any>[]) {
for (const data of dataToSave) {
const expressID = data.expressID;
if (!expressID) continue;
const { expressID } = data;
if (!expressID || expressID === -1) {
data.expressID = this.increaseMaxID(model);
}
await model.setProperties(expressID, data);
this.registerChange(model, expressID);
}
Expand Down Expand Up @@ -376,7 +378,11 @@ export class IfcPropertiesManager extends Component implements Disposable {
this.registerChange(model, psetID);
const indexer = this.components.get(IfcRelationsIndexer);
for (const expressID of expressIDs) {
indexer.addEntityRelations(model, expressID, "IsDefinedBy", psetID);
try {
indexer.addEntityRelations(model, expressID, "IsDefinedBy", psetID);
} catch (error: any) {
//
}
}
}

Expand Down Expand Up @@ -449,6 +455,28 @@ export class IfcPropertiesManager extends Component implements Disposable {
return modifiedIFC;
}

/**
* Retrieves all the entities of a specific type from the model and returns their express IDs wrapped in Handles.
* This is used to make references of an entity inside another entity attributes.
*
* @param model - The FragmentsGroup model from which to retrieve the entities.
* @param type - The type of the entities to retrieve. This should be the express ID of the IFC type.
*
* @returns A promise that resolves with an array of Handles, each containing the express ID of an entity of the specified type.
* @returns null if the model doesn't have any entity of that type
*/
async getEntityRef(model: FragmentsGroup, type: number) {
// This can be done very quickly if we add the expressID to IfcType map in FragmentsGroup
const entities = await model.getAllPropertiesOfType(type);
if (!entities) return null;
const handles: WEBIFC.Handle<unknown>[] = [];
for (const id in entities) {
const handle = new WEBIFC.Handle(Number(id));
handles.push(handle);
}
return handles;
}

/**
* Sets an attribute listener for a specific attribute of an entity in the model.
* The listener will trigger an event whenever the attribute's value changes.
Expand Down

0 comments on commit d464fe4

Please sign in to comment.