-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clean up excess geometry parts alpha API #115
Draft
MichaelBelousov
wants to merge
10
commits into
main
Choose a base branch
from
hack-patch-bad-view-filter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a214fd9
test phase 1
MichaelBelousov 8bb9ba8
test 2
MichaelBelousov 5f9c41f
test processAll instead
MichaelBelousov 1e208e0
add primitive geometry cleanup routine
MichaelBelousov b76b047
remove it.only
MichaelBelousov 4795804
Change files
MichaelBelousov a3026ed
Version bump [skip actions]
imodeljs-admin 03b792a
unlimited element id query
MichaelBelousov f8bbc7c
add cleanup option to test-app
MichaelBelousov b4878d3
Version bump [skip actions]
imodeljs-admin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@itwin-imodel-transformer-d56dd0fb-4a09-43f6-9ce5-9bc6a92c7880.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "add primitive geometry cleanup routine", | ||
"packageName": "@itwin/imodel-transformer", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { IModelDb } from "@itwin/core-backend"; | ||
import { DbResult, Id64String } from "@itwin/core-bentley"; | ||
import { ElementGeometry } from "@itwin/core-common"; | ||
|
||
/** | ||
* delete all geometry parts that are not referenced by any geometric elements. | ||
* This will be replaced by a more integrated approach | ||
* @internal | ||
*/ | ||
export function cleanupUnusedGeometryParts(db: IModelDb) { | ||
const usedGeomParts = new Set<Id64String>(); | ||
|
||
const allGeomElemIdsQuery = ` | ||
SELECT ECInstanceId | ||
FROM bis.GeometricElement | ||
`; | ||
db.withPreparedStatement(allGeomElemIdsQuery, (geomElemIdStmt) => { | ||
while (geomElemIdStmt.step() === DbResult.BE_SQLITE_ROW) { | ||
const geomElemId = geomElemIdStmt.getValue(0).getId(); | ||
db.elementGeometryRequest({ | ||
elementId: geomElemId, | ||
skipBReps: true, // breps contain no references to geometry parts | ||
onGeometry(geomInfo) { | ||
for (const entry of new ElementGeometry.Iterator(geomInfo)) { | ||
const maybeGeomPart = entry.toGeometryPart(); | ||
if (maybeGeomPart) | ||
usedGeomParts.add(maybeGeomPart); | ||
} | ||
}, | ||
}); | ||
} | ||
}); | ||
|
||
// NOTE: maybe (negligbly?) faster to do custom query with `NOT InVirtualSet()` | ||
const unusedGeomPartIds = db.queryEntityIds({ from: "bis.GeometryPart" }); | ||
for (const usedGeomPartId of usedGeomParts) | ||
unusedGeomPartIds.delete(usedGeomPartId); | ||
db.elements.deleteDefinitionElements([...unusedGeomPartIds]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,14 @@ | |
|
||
import { assert, expect } from "chai"; | ||
import * as fs from "fs"; | ||
import * as child_process from "child_process"; | ||
import * as path from "path"; | ||
import * as Semver from "semver"; | ||
import * as sinon from "sinon"; | ||
import { | ||
CategorySelector, DisplayStyle3d, DocumentListModel, Drawing, DrawingCategory, DrawingGraphic, DrawingModel, ECSqlStatement, Element, | ||
ElementMultiAspect, ElementOwnsChildElements, ElementOwnsExternalSourceAspects, ElementOwnsMultiAspects, ElementOwnsUniqueAspect, ElementRefersToElements, | ||
ElementUniqueAspect, ExternalSourceAspect, GenericPhysicalMaterial, GeometricElement, IModelDb, IModelElementCloneContext, IModelHost, IModelJsFs, | ||
ElementUniqueAspect, ExternalSourceAspect, GenericPhysicalMaterial, GeometricElement, GeometryPart, IModelDb, IModelElementCloneContext, IModelHost, IModelJsFs, | ||
InformationRecordModel, InformationRecordPartition, LinkElement, Model, ModelSelector, OrthographicViewDefinition, | ||
PhysicalModel, PhysicalObject, PhysicalPartition, PhysicalType, Relationship, RenderMaterialElement, RepositoryLink, Schema, SnapshotDb, SpatialCategory, StandaloneDb, | ||
SubCategory, Subject, Texture, | ||
|
@@ -22,9 +23,9 @@ import * as TestUtils from "../TestUtils"; | |
import { DbResult, Guid, Id64, Id64String, Logger, LogLevel, OpenMode } from "@itwin/core-bentley"; | ||
import { | ||
AxisAlignedBox3d, BriefcaseIdValue, Code, CodeScopeSpec, CodeSpec, ColorDef, CreateIModelProps, DefinitionElementProps, ElementAspectProps, ElementProps, | ||
ExternalSourceAspectProps, GeometricElement2dProps, ImageSourceFormat, IModel, IModelError, InformationPartitionElementProps, ModelProps, PhysicalElementProps, Placement3d, ProfileOptions, QueryRowFormat, RelatedElement, RelationshipProps, RepositoryLinkProps, | ||
ExternalSourceAspectProps, GeometricElement2dProps, GeometryPartProps, GeometryStreamBuilder, GeometryStreamProps, ImageSourceFormat, IModel, IModelError, InformationPartitionElementProps, ModelProps, PhysicalElementProps, Placement3d, ProfileOptions, QueryRowFormat, RelatedElement, RelationshipProps, RepositoryLinkProps, | ||
} from "@itwin/core-common"; | ||
import { Point3d, Range3d, StandardViewIndex, Transform, YawPitchRollAngles } from "@itwin/core-geometry"; | ||
import { Box, Point3d, Range3d, StandardViewIndex, Transform, Vector3d, YawPitchRollAngles } from "@itwin/core-geometry"; | ||
import { IModelExporter, IModelExportHandler, IModelTransformer, IModelTransformOptions, TransformerLoggerCategory } from "../../transformer"; | ||
import { | ||
AspectTrackingImporter, | ||
|
@@ -2635,6 +2636,97 @@ describe("IModelTransformer", () => { | |
targetDb.close(); | ||
}); | ||
|
||
function createBigGeomPart(): GeometryStreamProps { | ||
const geometryStreamBuilder = new GeometryStreamBuilder(); | ||
for (let i = 1; i < 2_00; ++i) { | ||
geometryStreamBuilder.appendGeometry(Box.createDgnBox( | ||
Point3d.createZero(), Vector3d.unitX(), Vector3d.unitY(), new Point3d(0, 0, i), | ||
i, i, i, i, true, | ||
)!); | ||
} | ||
return geometryStreamBuilder.geometryStream; | ||
} | ||
|
||
function createBoxWithGeomParts(geometryPartId: Id64String): GeometryStreamProps { | ||
const geometryStreamBuilder = new GeometryStreamBuilder(); | ||
geometryStreamBuilder.appendGeometry(Box.createDgnBox( | ||
Point3d.createZero(), Vector3d.unitX(), Vector3d.unitY(), new Point3d(0, 0, 0), | ||
0, 0, 0, 0, true, | ||
)!); | ||
geometryStreamBuilder.appendGeometryPart3d(geometryPartId); | ||
geometryStreamBuilder.appendGeometryPart3d(geometryPartId); | ||
return geometryStreamBuilder.geometryStream; | ||
} | ||
|
||
it("processAll prunes unnecessary geometry parts", async function () { | ||
const sourceDbFile = IModelTransformerTestUtils.prepareOutputFile("IModelTransformer", "PruneGeomParts.bim"); | ||
const sourceDb = SnapshotDb.createEmpty(sourceDbFile, { rootSubject: { name: "PruneGeomPartsSrc" } }); | ||
|
||
const sourceModelId = PhysicalModel.insert(sourceDb, IModel.rootSubjectId, "Physical"); | ||
const categoryId = SpatialCategory.insert(sourceDb, IModel.dictionaryId, "SpatialCategory", { color: ColorDef.green.toJSON() }); | ||
|
||
const [physObj1, physObj2] = [1, 2].map((i) => { | ||
const geometryPartProps: GeometryPartProps = { | ||
classFullName: GeometryPart.classFullName, | ||
model: IModelDb.dictionaryId, | ||
code: GeometryPart.createCode(sourceDb, IModelDb.dictionaryId, `GeometryPart${i}`), | ||
geom: createBigGeomPart(), | ||
}; | ||
|
||
const geomPartId = sourceDb.elements.insertElement(geometryPartProps); | ||
|
||
const physObjProps: PhysicalElementProps = { | ||
classFullName: PhysicalObject.classFullName, | ||
model: sourceModelId, | ||
category: categoryId, | ||
code: Code.createEmpty(), | ||
userLabel: "PhysicalObject1", | ||
geom: createBoxWithGeomParts(geomPartId), | ||
placement: { | ||
origin: Point3d.create(1, 1, 1), | ||
angles: YawPitchRollAngles.createDegrees(0, 0, 0), | ||
}, | ||
}; | ||
|
||
const physObjId = sourceDb.elements.insertElement(physObjProps); | ||
|
||
return { geomPartId, id: physObjId }; | ||
}); | ||
|
||
const targetDbFile: string = IModelTransformerTestUtils.prepareOutputFile("IModelTransformer", "PruneGeomParts-Target.bim"); | ||
const targetDb = SnapshotDb.createEmpty(targetDbFile, { rootSubject: { name: "PruneGeomParts" } }); | ||
targetDb.saveChanges(); | ||
|
||
const transformer = new IModelTransformer(sourceDb, targetDb, { cleanupUnusedGeometryParts: true }); | ||
// expect this to not reject, adding chai as promised makes the error less readable | ||
await transformer.processSchemas(); | ||
const targetModelId = PhysicalModel.insert(targetDb, IModel.rootSubjectId, "Physical"); | ||
|
||
const physObj1Elem = sourceDb.elements.getElement(physObj1.id); | ||
|
||
transformer.context.remapElement(physObj1Elem.model, targetModelId); | ||
transformer.shouldExportElement = (elem) => elem.id !== physObj2.id; | ||
await transformer.processAll(); | ||
|
||
function printSize(p: string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FIXME: remove from test |
||
// eslint-disable-next-line | ||
console.log(`Size of ${p}\n`, child_process.execSync(`du -h ${p}`, {encoding: "utf-8"})); | ||
} | ||
|
||
printSize(sourceDbFile); | ||
printSize(targetDbFile); | ||
|
||
assert(Id64.isValidId64(transformer.context.findTargetElementId(physObj1.id))); | ||
assert(!Id64.isValidId64(transformer.context.findTargetElementId(physObj2.id))); | ||
expect(count(sourceDb, GeometryPart.classFullName)).to.equal(2); | ||
expect(count(targetDb, GeometryPart.classFullName)).to.equal(1); | ||
|
||
// clean up | ||
transformer.dispose(); | ||
sourceDb.close(); | ||
targetDb.close(); | ||
}); | ||
|
||
/** unskip to generate a javascript CPU profile on just the processAll portion of an iModel */ | ||
it.skip("should profile an IModel transformation", async function () { | ||
const sourceDbFile = IModelTransformerTestUtils.prepareOutputFile("IModelTransformer", "ProfileTransformation.bim"); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move to importer