sgenoud / replicad

The library to build browser based 3D models with code.
https://replicad.xyz
MIT License
323 stars 38 forks source link

How to get the volume and surface area of a shape? #142

Open Niels-IO opened 4 months ago

Niels-IO commented 4 months ago

Hi @sgenoud,

Is there any way to get the volume and surface area of a solid shape with Replicad?

Thanks a lot!

kriho commented 3 months ago

I would also like to know if this is possible!

sgenoud commented 3 months ago

You could use opencascade directly to do this:

const {  getOC} = replicad;

export function measureVolume(shape) {
  const oc = getOC();
  const properties = new oc.GProp_GProps_1();
  oc.BRepGProp.VolumeProperties_1(
    shape.wrapped,
    properties,
    false,
    false,
  );

  const volume = properties.Mass();
  properties.delete();
  return volume;
}

Not very user friendly. So I added a set of measure helper to do that measureVolume, measureArea and measureLength. Make sure to have the latest version of the studio and use it on shapes!

kriho commented 2 months ago

Thanks a lot for implementing this directly! I tried using measureVolume() but the function never returned (maybe I was too impatient and it just takes minutes).