web-ifc-three icon indicating copy to clipboard operation
web-ifc-three copied to clipboard

How to reuse IFC models?

Open mysterybear opened this issue 3 years ago • 1 comments

Hi,

Could it be made clear somewhere how to reuse IFC models so that createSubset works?

Here's my code where I've been trying to get this going https://github.com/mysterybear/reuse-ifc-subsets-issue

I don't seem to be able to isolate the IFC loader or manager to concern itself with only the IFC for its particular component

IFCManager isn't exported either right? I would have tried hooking it up manually

mysterybear avatar Sep 29 '22 14:09 mysterybear

This was the same as #83 which is closed but I don't know if it should be... I've made the fix in my code here but it seems like a hacky workaround that shouldn't be necessary?

mysterybear avatar Oct 03 '22 13:10 mysterybear

Actually this doesn't fully resolve the problem

Highlighting works across many models now, but if you highlight 1 model of which there are many instances loaded, all models have that express ID highlighted :\

mysterybear avatar Oct 10 '22 13:10 mysterybear

@agviegas here's a minimal example (picking up from #92) of trying to add the same IFC twice:

https://github.com/mysterybear/many-ifc-test/blob/f4573928792c675390f6c969646f9d4a25b10409/src/App.tsx#L27-L75

this won't work, the IFC will only be part of the group it was most recently added to

the obvious solution here is to .clone() it which works and actually gives the cloned model a new model ID... So maybe this is sufficient to work with, like with raycasting and all, without any issues... But let me know if this is not the recommended approach please! And some docs/tutorial on recommended ways of working with many instances of a single IFC model etc would be great!

mysterybear avatar Oct 11 '22 15:10 mysterybear

Also if I log out the model ID's of: a) original loaded model b) the cloned model c) ifcLoader.ifcManager.state.models

there appear to be 3 different model ID's in play which is very confusing and not obvious how to work with subsets with these model ID's :(

mysterybear avatar Oct 11 '22 15:10 mysterybear

@agviegas we get this error when creating a subset with the model ID's as they are:

Uncaught TypeError: Cannot read properties of undefined (reading 'mesh')
    at ItemsMap.getGeometry (ItemsMap.ts:134:1)

This can be fixed like:

        if (!(found.object.modelID in ifc.state.models)) {
          ifc.state.models[found.object.modelID] = found.object
        }

as here https://github.com/mysterybear/many-ifc-test/blob/79e669ac4ded668c84bed0926b4968f29ab59bb9/src/App.tsx#L121-L123

is this a bug or is this how users are intended to use the library?

mysterybear avatar Oct 12 '22 09:10 mysterybear

Hey, sorry, but I don't quite get what you mean by "reuse IFC models". Also, did you check out our React examples? For instance, this one works well.

agviegas avatar Oct 17 '22 10:10 agviegas

from discord convo with @agviegas

If you want multiple instances of the same model, you can either load the same model twice, or create subsets of the whole model Probably the latter is more efficient

suggested some examples would be useful, hadn't thought of using subsets this way but tried a lot with loading the model, cloning etc and don't seem to be able to target model ID's correctly

mysterybear avatar Oct 17 '22 10:10 mysterybear

If you create a small, reproducible example we'll check it out.

agviegas avatar Oct 17 '22 10:10 agviegas

@agviegas what's wrong with https://github.com/mysterybear/many-ifc-test/blob/main/src/App.tsx ?

It's not even using React really, it just does everything within a useEffect hook... highlighting the first model ID works, highlighting the second one does not unless the lines that mutate the internal state are left uncommented here

https://github.com/mysterybear/many-ifc-test/blob/79e669ac4ded668c84bed0926b4968f29ab59bb9/src/App.tsx#L121-L123

mysterybear avatar Oct 17 '22 11:10 mysterybear

How many times is that useEffect function being called?

agviegas avatar Oct 17 '22 11:10 agviegas

How many times is that useEffect function being called?

in dev mode, twice; production once

I've added some logic to stop it running twice in dev mode, the behaviour with the IFC highlighting is unaffected

https://github.com/mysterybear/many-ifc-test/blob/main/src/App.tsx

mysterybear avatar Oct 17 '22 11:10 mysterybear

I've spotted that you are calling ifcLoader.load. You must await the end of the loading of a model before loading a new one. For that, you can use loader.loadAsync.

agviegas avatar Oct 22 '22 11:10 agviegas

@agviegas doesn't seem to make much difference

tried this: https://github.com/mysterybear/many-ifc-test/blob/18d596a29e178c56012f45980b5445f392a45726/src/App.tsx#L63-L86 which still clones so you get two models still, but highlighting only works on the one, so no change in behaviour

and this: https://github.com/mysterybear/many-ifc-test/blob/6bcf9424145b761a7c7f5ec859b0b898c7653ec5/src/App.tsx#L63-L86 which seems like a step backwards as only 1 model is rendered

mysterybear avatar Oct 24 '22 13:10 mysterybear

My bad, I thought you were trying to load two models. Just to be clear: you are trying to make a copy of a model you already loaded, right? If that's the case, you only have two options: load the model twice calling loadAsync twice, or create a subset of the whole model. You can't clone the three.js mesh and try to make subsets work with the copy.

agviegas avatar Oct 24 '22 15:10 agviegas

Just to be clear: you are trying to make a copy of a model you already loaded, right?

Yes :)

you only have two options: load the model twice calling loadAsync twice

Right, so that's like in this commit https://github.com/mysterybear/many-ifc-test/blob/6bcf9424145b761a7c7f5ec859b0b898c7653ec5/src/App.tsx#L63-L86 the behaviour I get is that it seems to only render 1 model under the latest place (scene or group) that it's been added

or create a subset of the whole model. You can't clone the three.js mesh and try to make subsets work with the copy.

Haven't actually tried this and recall you saying this is likely to perform better also. Will try to carve out some time with this approach. Still feeling strongly that a canonical example of how this problem is recommended to be solved would be awesome! :)

mysterybear avatar Oct 24 '22 15:10 mysterybear

oh crap there's a bug in there, I'm adding the first model twice, let me fix that

mysterybear avatar Oct 24 '22 15:10 mysterybear

Yeah you're right so this works now https://github.com/mysterybear/many-ifc-test/blob/b2bc56f0a6e0c7707357b5e790e616b3e9996065/src/App.tsx#L63-L86

Thanks

Haven't tried the subsets approach to this problem yet either, so that gives me some stuff to run with for now so will close for now, thanks :)

mysterybear avatar Oct 24 '22 20:10 mysterybear