diff --git a/cg/freecad/utils/freecad_to_json.py b/cg/freecad/utils/freecad_to_json.py index d1077c4..ec983e4 100644 --- a/cg/freecad/utils/freecad_to_json.py +++ b/cg/freecad/utils/freecad_to_json.py @@ -24,7 +24,9 @@ import Mesh import MeshPart import logging import math -from freecad.utils.is_object_solid import is_object_solid + +from freecad.utils.solid_tools import (is_object_solid, collect_clones) + from utils.custom_parser import CustomArgumentParser logger = logging.getLogger(__name__) @@ -39,6 +41,9 @@ def freecad_to_json(**kwargs): doc = FreeCAD.open(kwargs['fcstd_path']) docname = doc.Name + # collect equal cad objects + clones = collect_clones(doc): + # collect all materials fem_mats = [] for fem_mat in doc.Objects: diff --git a/cg/freecad/utils/is_object_solid.py b/cg/freecad/utils/is_object_solid.py deleted file mode 100644 index 4104263..0000000 --- a/cg/freecad/utils/is_object_solid.py +++ /dev/null @@ -1,29 +0,0 @@ -# coding: utf-8 -# Copyright (C) 2023 Ilia Kurochkin -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -''' -DESCRIPTION. -Simple FreeCAD's object test for manifold mawater-tight surface. -''' -__version__ = '0.2' -import FreeCAD - - -def is_object_solid(obj): - '''If obj is solid return True''' - if not isinstance(obj, FreeCAD.DocumentObject): - return False - - if not hasattr(obj, 'Shape'): - return False - - return obj.Shape.isClosed() diff --git a/cg/freecad/utils/solid_tools.py b/cg/freecad/utils/solid_tools.py new file mode 100644 index 0000000..74cc620 --- /dev/null +++ b/cg/freecad/utils/solid_tools.py @@ -0,0 +1,80 @@ +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# Copyright (C) 2021-2024 Robossembler LLC +# +# Created by Ilia Kurochkin (brothermechanic) +# contact: brothermechanic@yandex.com +# +# This file is part of Robossembler Framework +# project repo: https://gitlab.com/robossembler/framework +# +# Robossembler Framework is free software; +# you can redistribute it and/or modify +# it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 3 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . +# +# ***** END GPL LICENSE BLOCK ***** +# +# coding: utf-8 +''' +DESCRIPTION. +Solid tools for FreeCAD's .FCStd scene. +''' +__version__ = '0.2' + +import logging +import FreeCAD + +logger = logging.getLogger(__name__) +logging.basicConfig(level=logging.INFO) + + +def is_object_solid(obj) -> bool: + ''' + Simple FreeCAD's object test for manifold mawater-tight surface. + + :param obj: part, FreeCAD Part::Feature object + :returns: boolean of obj solid state + ''' + if not isinstance(obj, FreeCAD.DocumentObject): + return False + + if not hasattr(obj, 'Shape'): + return False + + return obj.Shape.isClosed() + + +def collect_clones(doc=FreeCAD.getDocument(FreeCAD.ActiveDocument.Label)) -> list: + ''' + This script find equal cad parts in a FreeCAD .FCStd scene. + + :param doc: document, freecad opened scene (or current scene) + :returns: list with clones sublists [['c0'], ['c1', 'c2', 'c4'],] + ''' + doc_clones = [] + for item in doc.Objects: + if not is_object_solid(item): + continue + item_clones = [] + item_clones.append(item.Label) + for other in doc.Objects: + if other == item: + continue + if other.Shape.isPartner(item.Shape): + logger.info('%s and %s objects has equal Shapes', + item.Label, other.Label) + item_clones.append(other.Label) + item_clones.sort() + if item_clones not in doc_clones: + doc_clones.append(item_clones) + return doc_clones