Skip to content

Commit

Permalink
Merge branch 'indexer'
Browse files Browse the repository at this point in the history
  • Loading branch information
HoyosJuan committed Aug 27, 2024
2 parents 5e50f16 + a2e592a commit 5082d75
Show file tree
Hide file tree
Showing 11 changed files with 503 additions and 72 deletions.
21 changes: 0 additions & 21 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,6 @@ <h3>Choose an example</h3>
<a style="width: fit-content;" href="./packages\core\src\core\Grids\example.html">core/Grids</a>
<a style="width: fit-content;" href="./packages\core\src\core\Cullers\example.html">core/Cullers</a>
<a style="width: fit-content;" href="./packages\core\src\core\Clipper\example.html">core/Clipper</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\openbim\BCFTopics\example.html">core/BCFTopics</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\measurement\MeasurementUtils\example.html">core/MeasurementUtils</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\ifc\IfcRelationsIndexer\example.html">core/IfcRelationsIndexer</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\ifc\IfcPropertiesManager\example.html">core/IfcPropertiesManager</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\ifc\IfcJsonExporter\example.html">core/IfcJsonExporter</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\fragments\IfcPropertiesTiler\example.html">core/IfcPropertiesTiler</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\fragments\IfcLoader\example.html">core/IfcLoader</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\fragments\IfcGeometryTiler\example.html">core/IfcGeometryTiler</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\fragments\Hider\example.html">core/Hider</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\fragments\FragmentsManager\example.html">core/FragmentsManager</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\fragments\Exploder\example.html">core/Exploder</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\fragments\Classifier\example.html">core/Classifier</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\fragments\BoundingBoxer\example.html">core/BoundingBoxer</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\core\Worlds\example.html">core/Worlds</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\core\ShadowedScene\example.html">core/ShadowedScene</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\core\Raycasters\example.html">core/Raycasters</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\core\OrthoPerspectiveCamera\example.html">core/OrthoPerspectiveCamera</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\core\MiniMap\example.html">core/MiniMap</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\core\Grids\example.html">core/Grids</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\core\Cullers\example.html">core/Cullers</a>
<a style="width: fit-content;" href="./packages\core\node_modules\@thatopen\components\src\core\Clipper\example.html">core/Clipper</a>

</div>
</body>
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/core/Types/src/data-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export class DataMap<K, V> extends Map<K, V> {
*/
set(key: K, value: V) {
const triggerUpdate = this.has(key);
const isValid = this.guard(key, value);
const guard = this.guard ?? (() => true);
const isValid = guard(key, value);
if (!isValid) return this;
const result = super.set(key, value);
if (triggerUpdate) {
Expand Down
138 changes: 104 additions & 34 deletions packages/core/src/ifc/IfcPropertiesManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Component, Disposable, Event, Components } from "../../core";
import { IfcPropertiesUtils } from "../Utils";
import { IfcLoader } from "../../fragments/IfcLoader";
import { UUID } from "../../utils";
import { IfcRelationsIndexer } from "../IfcRelationsIndexer";
import { IfcRelation, IfcRelationsIndexer } from "../IfcRelationsIndexer";
import { ifcRelAttrsPosition, ifcRelClassNames } from "./src";

/**
* Types for boolean properties in IFC schema.
Expand Down Expand Up @@ -183,7 +184,7 @@ export class IfcPropertiesManager extends Component implements Disposable {
data.expressID = this.getNewExpressID(model);
}
await model.setProperties(data.expressID, data);
this.registerChange(model, expressID);
this.registerChange(model, data.expressID);
}
}

Expand Down Expand Up @@ -357,33 +358,21 @@ export class IfcPropertiesManager extends Component implements Disposable {
this.registerChange(model, psetID, propID);
}

async addElementToPset(
/**
* @deprecated Use indexer.addEntitiesRelation instead. This will be removed in future releases.
*/
addElementToPset(
model: FragmentsGroup,
psetID: number,
...expressIDs: number[]
) {
const relID = await IfcPropertiesUtils.getPsetRel(model, psetID);
if (!relID) return;
const rel = await model.getProperties(relID);
if (!rel) return;
for (const expressID of expressIDs) {
const elementHandle = new WEBIFC.Handle(expressID);
rel.RelatedObjects.push(elementHandle);
this.onElementToPset.trigger({
model,
psetID,
elementID: expressID,
});
}
this.registerChange(model, psetID);
const indexer = this.components.get(IfcRelationsIndexer);
for (const expressID of expressIDs) {
try {
indexer.addEntityRelations(model, expressID, "IsDefinedBy", psetID);
} catch (error: any) {
//
}
}
indexer.addEntitiesRelation(
model,
psetID,
{ type: WEBIFC.IFCRELDEFINESBYPROPERTIES, inv: "IsDefinedBy" },
...expressIDs,
);
}

/**
Expand Down Expand Up @@ -417,6 +406,91 @@ export class IfcPropertiesManager extends Component implements Disposable {
this.registerChange(model, psetID);
}

/**
* Creates a new instance of a relationship between entities in the IFC model.
*
* @param model - The FragmentsGroup model in which to create the relationship.
* @param type - The type of the relationship to create.
* @param relatingID - The express ID of the entity that is related to the other entities.
* @param relatedIDs - The express IDs of the entities that are related to the relating entity.
*
* @returns A promise that resolves with the newly created relationship.
*
* @throws Will throw an error if the relationship type is unsupported.
*/
async createIfcRel(
model: FragmentsGroup,
type: IfcRelation,
relatingID: number,
relatedIDs: number[],
) {
const relName = ifcRelClassNames[type];
if (!relName) {
throw new Error(`IfcPropertiesManager: ${relName} is unsoported.`);
}

const schema = model.ifcMetadata.schema;
const attributePositions = ifcRelAttrsPosition[relName];
// @ts-ignore safe to use ts-ignore as we are checking in the following line if the class exists.
const RelClass = WEBIFC[schema][relName];
if (!(attributePositions && RelClass)) {
throw new Error(`IfcPropertiesManager: ${relName} is unsoported.`);
}

const args: any[] = [new WEBIFC[schema].IfcGloballyUniqueId(UUID.create())];

// const { related, relating } = attributePositions;

// if (related < relating) {
// for (let i = 1; i < related - 1; i++) args.push(null);
// const relatedIDsSet = new Set(relatedIDs);
// const relatingHandles = [...relatedIDsSet].map(
// (expressID) => new WEBIFC.Handle(expressID),
// );
// args.push(relatingHandles);

// for (let i = related; i < relating - 1; i++) args.push(null);
// args.push(new WEBIFC.Handle(relatingID));
// } else {
// for (let i = 1; i < relating - 1; i++) args.push(null);
// for (let i = relating; i < related - 1; i++) args.push(null);
// args.push(new WEBIFC.Handle(relatingID));

// const relatedIDsSet = new Set(relatedIDs);
// const relatingHandles = [...relatedIDsSet].map(
// (expressID) => new WEBIFC.Handle(expressID),
// );
// args.push(relatingHandles);
// }

const { related, relating } = attributePositions;
const relatedIDsSet = new Set(relatedIDs);
const relatingHandles = [...relatedIDsSet].map(
(expressID) => new WEBIFC.Handle(expressID),
);

const addNulls = (start: number, end: number) => {
for (let i = start; i < end - 1; i++) args.push(null);
};

if (related < relating) {
addNulls(1, related);
args.push(relatingHandles);
addNulls(related, relating);
args.push(new WEBIFC.Handle(relatingID));
} else {
addNulls(1, relating);
addNulls(relating, related);
args.push(new WEBIFC.Handle(relatingID));
args.push(relatingHandles);
}

// @ts-ignore
const ifcRel = new RelClass(...args);
await this.setData(model, ifcRel);
return ifcRel;
}

/**
* Saves the changes made to the model to a new IFC file.
*
Expand All @@ -431,21 +505,15 @@ export class IfcPropertiesManager extends Component implements Disposable {
const ifcLoader = this.components.get(IfcLoader);
const ifcApi = ifcLoader.webIfc;
const modelID = await ifcLoader.readIfcFile(ifcToSaveOn);
const indexer = this.components.get(IfcRelationsIndexer);
await indexer.applyRelationChanges();
const changes = this.changeMap[model.uuid] ?? [];
for (const expressID of changes) {
const data = (await model.getProperties(expressID)) as any;
if (!data) {
try {
ifcApi.DeleteLine(modelID, expressID);
} catch (err) {
// Nothing here...
}
ifcApi.DeleteLine(modelID, expressID);
} else {
try {
ifcApi.WriteLine(modelID, data);
} catch (err) {
// Nothing here...
}
ifcApi.WriteLine(modelID, data);
}
}
const modifiedIFC = ifcApi.SaveModel(modelID);
Expand Down Expand Up @@ -596,3 +664,5 @@ export class IfcPropertiesManager extends Component implements Disposable {
return prop;
}
}

export * from "./src";
2 changes: 2 additions & 0 deletions packages/core/src/ifc/IfcPropertiesManager/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./rels-builder";
export * from "./types";
55 changes: 55 additions & 0 deletions packages/core/src/ifc/IfcPropertiesManager/src/rels-builder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as WEBIFC from "web-ifc";
import { IfcRelName } from "./types";
import { IfcRelation } from "../../IfcRelationsIndexer";

type IfcRelAttributePosition = { related: number; relating: number };

export const ifcRelAttrsPosition: Record<IfcRelName, IfcRelAttributePosition> =
{
// IfcRelAssigns
IfcRelAssignsToControl: { related: 5, relating: 7 },
IfcRelAssignsToGroup: { related: 5, relating: 7 },
IfcRelAssignsToProduct: { related: 5, relating: 7 },
// IfcRelAssociates
IfcRelAssociatesClassification: { related: 5, relating: 6 },
IfcRelAssociatesMaterial: { related: 5, relating: 6 },
IfcRelAssociatesDocument: { related: 5, relating: 6 },
// IfcRelConnects
IfcRelContainedInSpatialStructure: { related: 5, relating: 6 },
IfcRelFlowControlElements: { related: 5, relating: 6 },
IfcRelConnectsElements: { related: 7, relating: 6 },
// IfcRelDeclares
IfcRelDeclares: { related: 6, relating: 5 },
// IfcRelDecomposes
IfcRelAggregates: { related: 6, relating: 5 },
IfcRelNests: { related: 6, relating: 5 },
// IfcRelDefines
IfcRelDefinesByProperties: { related: 5, relating: 6 },
IfcRelDefinesByType: { related: 5, relating: 6 },
IfcRelDefinesByTemplate: { related: 5, relating: 6 },
};

export const ifcRelClassNames: Record<IfcRelation, IfcRelName> = {
// IfcRelAssigns
[WEBIFC.IFCRELASSIGNSTOCONTROL]: "IfcRelAssignsToControl",
[WEBIFC.IFCRELASSIGNSTOGROUP]: "IfcRelAssignsToGroup",
[WEBIFC.IFCRELASSIGNSTOPRODUCT]: "IfcRelAssignsToProduct",
// IfcRelAssociates
[WEBIFC.IFCRELASSOCIATESCLASSIFICATION]: "IfcRelAssociatesClassification",
[WEBIFC.IFCRELASSOCIATESMATERIAL]: "IfcRelAssociatesMaterial",
[WEBIFC.IFCRELASSOCIATESDOCUMENT]: "IfcRelAssociatesDocument",
// IfcRelConnects
[WEBIFC.IFCRELCONTAINEDINSPATIALSTRUCTURE]:
"IfcRelContainedInSpatialStructure",
[WEBIFC.IFCRELCONNECTSELEMENTS]: "IfcRelConnectsElements",
[WEBIFC.IFCRELFLOWCONTROLELEMENTS]: "IfcRelFlowControlElements",
// IfcRelDeclares
[WEBIFC.IFCRELDECLARES]: "IfcRelDeclares",
// IfcRelDecomposes
[WEBIFC.IFCRELAGGREGATES]: "IfcRelAggregates",
[WEBIFC.IFCRELNESTS]: "IfcRelNests",
// IfcRelDefines
[WEBIFC.IFCRELDEFINESBYPROPERTIES]: "IfcRelDefinesByProperties",
[WEBIFC.IFCRELDEFINESBYTYPE]: "IfcRelDefinesByType",
[WEBIFC.IFCRELDEFINESBYTEMPLATE]: "IfcRelDefinesByTemplate",
};
25 changes: 25 additions & 0 deletions packages/core/src/ifc/IfcPropertiesManager/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export type IfcRelationNames = [
// IfcRelAssigns
"IfcRelAssignsToControl",
"IfcRelAssignsToGroup",
"IfcRelAssignsToProduct",
// IfcRelAssociates
"IfcRelAssociatesClassification",
"IfcRelAssociatesMaterial",
"IfcRelAssociatesDocument",
// IfcRelConnects
"IfcRelContainedInSpatialStructure",
"IfcRelFlowControlElements",
"IfcRelConnectsElements",
// IfcRelDeclares
"IfcRelDeclares",
// IfcRelDecomposes
"IfcRelAggregates",
"IfcRelNests",
// IfcRelDefines
"IfcRelDefinesByProperties",
"IfcRelDefinesByType",
"IfcRelDefinesByTemplate",
];

export type IfcRelName = IfcRelationNames[number];
Loading

0 comments on commit 5082d75

Please sign in to comment.