pub struct Cad { /* private fields */ }Expand description
A handle to a loaded CAD model.
Dropping a Cad releases the underlying native resources; there is no
explicit close (unlike the Go bindings, which need one). All query methods
are blocking — wrap them in tokio::task::spawn_blocking from async code.
Implementations§
Source§impl Cad
impl Cad
Sourcepub fn load<T: Serialize + ?Sized>(entry: &T) -> Result<Self, CadLibError>
pub fn load<T: Serialize + ?Sized>(entry: &T) -> Result<Self, CadLibError>
Load a CAD model from the CAD data map entry of a workflow method’s run
request, or anything else serializing to the same shape (a
serde_json::Value, a map, a struct of your own).
Only the entry’s externalReference is required; dataType and
source.type are validated when present and other keys are ignored.
let cad = Cad::load(&request.data_map.cad)?;Raw JSON text has to be parsed first (serde_json::from_str::<Value>):
a &str serializes to a JSON string rather than an object, which is
rejected with invalid-cad-model-json.
§Errors
Returns CadLibError::Expected when the entry is rejected (no or
malformed reference, an unsupported data type or source) and
CadLibError::Unexpected when the entry cannot be serialized, contains
an interior NUL byte, or the native library is incompatible with these
bindings. The error codes it can return are listed in
https://github.com/SimScaleGmbH/simscale-rust-cadlib/blob/main/ERRORS.md.
Source§impl Cad
impl Cad
Sourcepub fn tessellate(
&self,
dest_dir: &str,
params: Option<&TessellateParameters>,
) -> Result<TessellateResponse, CadLibError>
pub fn tessellate( &self, dest_dir: &str, params: Option<&TessellateParameters>, ) -> Result<TessellateResponse, CadLibError>
Tessellates the model and writes the STL files into a directory.
One STL file is written per region by default, or one per face when
write_one_file_per_face is set. The directory is created if it does not
exist, and files in it with the same names are overwritten.
tessellation_type, max_facet_width and allowed_facet_faults_level
apply to Parasolid models, and tessellation_type has to be set explicitly;
feature_size, adaptivity and max_edge_length apply to nTop models
and all default when omitted. tess_surface_area is set in the response
only when calculate_area is requested.
Supported for Parasolid, STL and nTop models.
§Errors
Returns CadLibError::Expected for a known domain error and
CadLibError::Unexpected if dest_dir contains an interior NUL byte,
the parameters cannot be serialized, or the library fails internally.
§Query errors
tessellation-has-error[-with-entity]- The kernel could not tessellate one or more entities.tessellation-ntop-negative-adaptivity-adaptivityis negative.tessellation-ntop-zero-edge-length-max_edge_lengthis zero.tessellation-ntop-zero-feature-size-feature_sizeis zero.tessellation-too-large- The result could not be written; the kernel ran out of disk space.
Sourcepub fn tessellate_faces(
&self,
dest_dir: &str,
params: Option<&TessellateFacesParameters>,
) -> Result<TessellateFacesResponse, CadLibError>
pub fn tessellate_faces( &self, dest_dir: &str, params: Option<&TessellateFacesParameters>, ) -> Result<TessellateFacesResponse, CadLibError>
Tessellates the requested faces, writing one STL file per face into a directory.
Each requested face is tessellated into its own STL file, named after the face id; a face requested more than once is tessellated only once. The response additionally carries the normal of each face, evaluated at the mid point of the face.
The faces to tessellate are required, so a query without parameters is rejected. The directory is created if it does not exist, and files in it with the same names are overwritten.
Supported only for Parasolid models. Other formats fail with
query-type-unsupported-for-cad-format.
§Errors
Returns CadLibError::Expected for a known domain error and
CadLibError::Unexpected if dest_dir contains an interior NUL byte,
the parameters cannot be serialized, or the library fails internally.
§Query errors
tessellation-has-error- The kernel could not tessellate one of the faces.
Sourcepub fn generate_comfort_map_disc(
&self,
dest_dir: &str,
params: Option<&GenerateComfortMapDiscParameters>,
) -> Result<GenerateComfortMapDiscResponse, CadLibError>
pub fn generate_comfort_map_disc( &self, dest_dir: &str, params: Option<&GenerateComfortMapDiscParameters>, ) -> Result<GenerateComfortMapDiscResponse, CadLibError>
Generates the comfort-map disc and writes the STL result into a directory.
The disc is a tessellated surface covering a region of interest: a circle of
radius floor_radius around center, placed height above the ground.
fineness controls how finely it is tessellated.
By default the disc is written as a single file. Asking for one of the zipped
encodings in result_encoding unpacks the result into the directory instead.
The directory is created if it does not exist, and files in it with
the same names are overwritten.
A disc with no geometry is reported as empty-comfort-map-disc.
Supported for Parasolid and STL models. Other formats fail with
query-type-unsupported-for-cad-format.
§Errors
Returns CadLibError::Expected for a known domain error and
CadLibError::Unexpected if dest_dir contains an interior NUL byte,
the parameters cannot be serialized, or the library fails internally.
§Query errors
empty-comfort-map-disc- The query returned no geometry.lawson-cylinder-exceeds-size-box-floor_radiusis too large for the model’s size box.lawson-negative-height-above-ground-heightis negative.lawson-roi-radius-must-be-positive-floor_radiusis zero or negative.lawson-tessellation-max-width-must-be-positive-finenessis zero or negative.lawson-tessellation-too-fine-finenessproduces more than 100 million triangles, which the query does not allow.tessellation-has-error[-with-entity]- The kernel could not tessellate one or more entities.tessellation-too-large- The result could not be written; the kernel ran out of disk space.
Sourcepub fn volume(
&self,
params: Option<&VolumeParameters>,
) -> Result<VolumeResponse, CadLibError>
pub fn volume( &self, params: Option<&VolumeParameters>, ) -> Result<VolumeResponse, CadLibError>
Computes the volume of each selected solid, and their total.
The solids and sheets to measure are given by their entity ids, as
entities returns them.
volumes holds one entry per measured entity, keyed by its entity id,
and total is their sum.
Supported for Parasolid and nTop models. Other formats fail with
query-type-unsupported-for-cad-format.
§Errors
Returns CadLibError::Expected for a known domain error and
CadLibError::Unexpected if the parameters cannot be serialized or the
library fails internally.
Sourcepub fn bounding_box(
&self,
params: Option<&BoundingBoxParameters>,
) -> Result<BoundingBoxResponse, CadLibError>
pub fn bounding_box( &self, params: Option<&BoundingBoxParameters>, ) -> Result<BoundingBoxResponse, CadLibError>
Computes the bounding box of the model, of each body, and of any faces given.
The box of the whole model and one box per solid and sheet are always returned. Naming faces additionally returns one box per face, keyed by face id, and a single box enclosing all of them.
Supported for Parasolid and nTop models. Other formats fail with
query-type-unsupported-for-cad-format.
§Errors
Returns CadLibError::Expected for a known domain error and
CadLibError::Unexpected if the parameters cannot be serialized or the
library fails internally.
Sourcepub fn center_of_gravity(
&self,
params: Option<&CenterOfGravityParameters>,
) -> Result<CenterOfGravityResponse, CadLibError>
pub fn center_of_gravity( &self, params: Option<&CenterOfGravityParameters>, ) -> Result<CenterOfGravityResponse, CadLibError>
Computes the center of gravity of the given faces and edges.
The selection accepts faces and edges only; any other entity type fails the query. One center of gravity is computed for the selection as a whole, not one per selected entity.
Supported only for Parasolid models. Other formats fail with
query-type-unsupported-for-cad-format.
§Errors
Returns CadLibError::Expected for a known domain error and
CadLibError::Unexpected if the parameters cannot be serialized or the
library fails internally.
§Query errors
compute-cog-entity-not-supported- A selected entity has unsupported type, it is neither a face nor an edge.
Sourcepub fn detect_enclosure(
&self,
params: Option<&DetectEnclosureParameters>,
) -> Result<DetectEnclosureResponse, CadLibError>
pub fn detect_enclosure( &self, params: Option<&DetectEnclosureParameters>, ) -> Result<DetectEnclosureResponse, CadLibError>
Finds the solids that enclose a given point.
The response holds the id of every solid that contains it.
Supported only for Parasolid models. Other model formats fail with
query-type-unsupported-for-cad-format.
§Errors
Returns CadLibError::Expected for a known domain error and
CadLibError::Unexpected if the parameters cannot be serialized or the
library fails internally.
Sourcepub fn detect_common_rotation(
&self,
params: Option<&DetectCommonRotationParameters>,
) -> Result<DetectCommonRotationResponse, CadLibError>
pub fn detect_common_rotation( &self, params: Option<&DetectCommonRotationParameters>, ) -> Result<DetectCommonRotationResponse, CadLibError>
Detects the rotation axis and center shared by the given faces.
All the given faces must be rotationally aligned about a single axis; the query fails if they are not, or if no axis can be determined. The response gives the direction of that axis and a point on it.
Supported only for Parasolid models. Other formats fail with
query-type-unsupported-for-cad-format.
§Errors
Returns CadLibError::Expected for a known domain error and
CadLibError::Unexpected if the parameters cannot be serialized or the
library fails internally.
§Query errors
detect-rotation-axis-not-consistent-for-all-faces- The selected faces do not share the same rotation axis.detect-rotation-could-not-detect-axis- No rotation axis could be detected for the selected faces.detect-rotation-flat-faces-selected[-with-entity]- The selection contains flat faces, which have no rotation axis.detect-rotation-multiple-possible-directions- More than one rotation direction fits the selected faces.
Sourcepub fn colors(
&self,
params: Option<&ColorsParameters>,
) -> Result<ColorsResponse, CadLibError>
pub fn colors( &self, params: Option<&ColorsParameters>, ) -> Result<ColorsResponse, CadLibError>
Returns the colors defined on the model’s solids and sheets.
The query takes no parameters and covers the whole model. Each entry names
the entity the color is defined on and the color itself, as a hex string
such as #ff0000.
Supported for Parasolid and nTop models. Other formats fail with
query-type-unsupported-for-cad-format.
§Errors
Returns CadLibError::Expected for a known domain error and
CadLibError::Unexpected if the parameters cannot be serialized or the
library fails internally.
Sourcepub fn material_points(
&self,
params: Option<&MaterialPointsParameters>,
) -> Result<MaterialPointsResponse, CadLibError>
pub fn material_points( &self, params: Option<&MaterialPointsParameters>, ) -> Result<MaterialPointsResponse, CadLibError>
Computes a point inside the material, for the whole model or for each solid.
query_type chooses between the two. The response sets the field that
matches it and leaves the other unset; the per-body field is keyed by
entity id.
A material point is only guaranteed to lie inside the material. Which interior point is returned is unspecified, and can differ between runs.
Supported only for Parasolid models. Other formats fail with
query-type-unsupported-for-cad-format.
§Errors
Returns CadLibError::Expected for a known domain error and
CadLibError::Unexpected if the parameters cannot be serialized or the
library fails internally.
Sourcepub fn planar_patch_primitives(
&self,
params: Option<&PlanarPatchPrimitivesParameters>,
) -> Result<PlanarPatchPrimitivesResponse, CadLibError>
pub fn planar_patch_primitives( &self, params: Option<&PlanarPatchPrimitivesParameters>, ) -> Result<PlanarPatchPrimitivesResponse, CadLibError>
Approximates groups of coplanar faces with a circle or rectangle primitive.
Groups the requested faces into patches of adjacent, coplanar faces and approximates each patch with the primitive — a circle or a rectangle — whose area is closest to the area of the patch. All faces of a patch belong to the same occurrence. The query fails if any of the requested faces is not planar.
Supported only for Parasolid models. Other formats fail with
query-type-unsupported-for-cad-format.
§Errors
Returns CadLibError::Expected for a known domain error and
CadLibError::Unexpected if the parameters cannot be serialized or the
library fails internally.
§Query errors
planar-patch-primitives-degenerate-patch[-with-entity]- A face has no interior point, or a patch is not two-dimensional, so no primitive fits it.planar-patch-primitives-non-coplanar-faces[-with-entity]- Faces that are adjacent, and so belong to the same patch, lie on different planes.planar-patch-primitives-non-planar-faces[-with-entity]- One or more of the requested faces is not planar.
Sourcepub fn model_metrics(
&self,
params: Option<&ModelMetricsParameters>,
) -> Result<ModelMetricsResponse, CadLibError>
pub fn model_metrics( &self, params: Option<&ModelMetricsParameters>, ) -> Result<ModelMetricsResponse, CadLibError>
Computes volume and surface-area metrics for the model and its entities.
Computes aggregated volume and surface area for the model, along with per-solid, per-sheet, per-face and per-edge metrics.
The query takes no parameters: the metrics always cover the whole model.
Supported for Parasolid and nTop models. Other formats fail with
query-type-unsupported-for-cad-format.
§Errors
Returns CadLibError::Expected for a known domain error and
CadLibError::Unexpected if the parameters cannot be serialized or the
library fails internally.
Sourcepub fn entities(&self) -> Result<EntitiesResponse, CadLibError>
pub fn entities(&self) -> Result<EntitiesResponse, CadLibError>
Returns the model’s entities: solids, sheets, faces, edges and vertices.
The list is flat: each entity references its parents and children by their 0-based index in it. Solids and sheets are the top-level entities.
Each entity carries the entity id that the other queries take as input.
Supported for Parasolid, STL and nTop models.
§Errors
Returns CadLibError::Expected for a known domain error and
CadLibError::Unexpected if the library fails internally.