xryv commited on
Commit
fd4e6a2
·
1 Parent(s): ef27986
README.md CHANGED
@@ -12,3 +12,25 @@ short_description: 'Comparing Step Files and Find a Similarity ranking '
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
15
+
16
+ # STEP File Comparator
17
+
18
+ A state-of-the-art web-based application to compare two STEP files and rank their similarity based on geometric and topological features.
19
+
20
+ ## Features
21
+ - Upload and process STEP files.
22
+ - Compute geometric properties like volume, surface area, and bounding box dimensions.
23
+ - Advanced similarity metrics based on shape features.
24
+ - Interactive 3D visualization using `three.js`.
25
+
26
+ ## Prerequisites
27
+ - Python 3.8+
28
+ - Conda (preferred) or Pip
29
+ - Node.js (for visualization)
30
+
31
+ ## Installation
32
+
33
+ ### Clone the Repository
34
+ ```bash
35
+ git clone https://github.com/username/stepfile_compare.git
36
+ cd stepfile_compare
app.py CHANGED
@@ -1,7 +1,6 @@
1
- import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
1
+ from app import create_app
2
 
3
+ app = create_app()
 
4
 
5
+ if __name__ == "__main__":
6
+ app.run(host="0.0.0.0", port=3001, debug=True)
app/__init__.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask
2
+
3
+ def create_app():
4
+ app = Flask(__name__)
5
+ app.config["UPLOAD_FOLDER"] = "uploads"
6
+
7
+ # Register Blueprints
8
+ from .routes import main
9
+ app.register_blueprint(main)
10
+
11
+ return app
app/routes.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from flask import Blueprint, request, jsonify, render_template
3
+ from .utils.step_utils import load_step, compute_properties, compare_shapes
4
+
5
+ main = Blueprint("main", __name__)
6
+
7
+ @main.route("/", methods=["GET"])
8
+ def index():
9
+ return render_template("index.html")
10
+
11
+ @main.route("/compare", methods=["POST"])
12
+ def compare():
13
+ files = request.files.getlist("files")
14
+ if len(files) != 2:
15
+ return jsonify({"error": "Two STEP files are required"}), 400
16
+
17
+ try:
18
+ # Save uploaded files temporarily
19
+ file_paths = []
20
+ for file in files:
21
+ path = os.path.join("uploads", file.filename)
22
+ file.save(path)
23
+ file_paths.append(path)
24
+
25
+ # Parse and compute properties
26
+ shape1 = load_step(file_paths[0])
27
+ shape2 = load_step(file_paths[1])
28
+ props1 = compute_properties(shape1)
29
+ props2 = compute_properties(shape2)
30
+
31
+ # Perform advanced comparison
32
+ similarity_score = compare_shapes(shape1, shape2)
33
+
34
+ # Cleanup uploaded files
35
+ for path in file_paths:
36
+ os.remove(path)
37
+
38
+ return jsonify({
39
+ "file1": props1,
40
+ "file2": props2,
41
+ "similarity_score": similarity_score
42
+ })
43
+ except Exception as e:
44
+ return jsonify({"error": str(e)}), 500
app/templates/index.html ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>STEP File Comparator</title>
5
+ </head>
6
+ <body>
7
+ <h1>STEP File Comparator</h1>
8
+ <form id="uploadForm" enctype="multipart/form-data">
9
+ <input type="file" name="files" multiple accept=".step,.stp" required>
10
+ <button type="submit">Compare</button>
11
+ </form>
12
+ <div id="results"></div>
13
+ <script src="/static/js/visualization.js"></script>
14
+ <script>
15
+ document.getElementById("uploadForm").onsubmit = async (e) => {
16
+ e.preventDefault();
17
+ const formData = new FormData(e.target);
18
+ const response = await fetch("/compare", {
19
+ method: "POST",
20
+ body: formData,
21
+ });
22
+ const result = await response.json();
23
+ document.getElementById("results").innerText = JSON.stringify(result, null, 2);
24
+ };
25
+ </script>
26
+ </body>
27
+ </html>
app/utils/geometry_utils.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from OCC.Core.BRep import BRep_Tool
2
+ from OCC.Core.BRepExtrema import BRepExtrema_DistShapeShape
3
+ from OCC.Core.TopExp import TopExp_Explorer
4
+ from OCC.Core.TopAbs import TopAbs_VERTEX, TopAbs_EDGE
5
+ from OCC.Core.gp import gp_Pnt
6
+
7
+ def compute_centroid(shape):
8
+ """Calculate the centroid of a shape."""
9
+ extrema = BRepExtrema_DistShapeShape(shape, shape)
10
+ extrema.Perform()
11
+ if extrema.IsDone():
12
+ p1 = extrema.PointOnShape1(1)
13
+ return {"x": p1.X(), "y": p1.Y(), "z": p1.Z()}
14
+ return {"x": 0, "y": 0, "z": 0}
15
+
16
+ def count_features(shape):
17
+ """Count specific geometric features like vertices and edges."""
18
+ vertex_count = 0
19
+ edge_count = 0
20
+
21
+ # Count vertices
22
+ explorer = TopExp_Explorer(shape, TopAbs_VERTEX)
23
+ while explorer.More():
24
+ vertex_count += 1
25
+ explorer.Next()
26
+
27
+ # Count edges
28
+ explorer = TopExp_Explorer(shape, TopAbs_EDGE)
29
+ while explorer.More():
30
+ edge_count += 1
31
+ explorer.Next()
32
+
33
+ return {"vertices": vertex_count, "edges": edge_count}
app/utils/similarity_utils.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def compare_shapes(shape1, shape2):
2
+ props1 = compute_properties(shape1)
3
+ props2 = compute_properties(shape2)
4
+
5
+ # Volume similarity
6
+ volume_similarity = min(props1["volume"], props2["volume"]) / max(props1["volume"], props2["volume"])
7
+
8
+ # Surface similarity
9
+ surface_similarity = min(props1["surface_area"], props2["surface_area"]) / max(props1["surface_area"], props2["surface_area"])
10
+
11
+ # Bounding box similarity
12
+ bbox_similarity = 1 - (
13
+ abs(props1["bounding_box"]["x_length"] - props2["bounding_box"]["x_length"]) +
14
+ abs(props1["bounding_box"]["y_length"] - props2["bounding_box"]["y_length"]) +
15
+ abs(props1["bounding_box"]["z_length"] - props2["bounding_box"]["z_length"])
16
+ ) / sum(props1["bounding_box"].values())
17
+
18
+ # Overall similarity score
19
+ return (volume_similarity + surface_similarity + bbox_similarity) / 3
app/utils/step_utils.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from OCC.Core.STEPControl import STEPControl_Reader
2
+ from OCC.Core.BRepGProp import brepgprop_SurfaceProperties, brepgprop_VolumeProperties
3
+ from OCC.Core.GProp import GProp_GProps
4
+ from OCC.Core.Bnd import Bnd_Box
5
+ from OCC.Core.BRepBndLib import brepbndlib_Add
6
+
7
+ def load_step(file_path):
8
+ reader = STEPControl_Reader()
9
+ status = reader.ReadFile(file_path)
10
+ if status != 1:
11
+ raise ValueError("Invalid STEP file")
12
+ reader.TransferRoots()
13
+ return reader.OneShape()
14
+
15
+ def compute_properties(shape):
16
+ props = GProp_GProps()
17
+
18
+ # Surface area and volume
19
+ brepgprop_SurfaceProperties(shape, props)
20
+ surface_area = props.Mass()
21
+ brepgprop_VolumeProperties(shape, props)
22
+ volume = props.Mass()
23
+
24
+ # Bounding box
25
+ bbox = Bnd_Box()
26
+ brepbndlib_Add(shape, bbox)
27
+ xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get()
28
+ dimensions = {
29
+ "x_length": xmax - xmin,
30
+ "y_length": ymax - ymin,
31
+ "z_length": zmax - zmin
32
+ }
33
+
34
+ return {
35
+ "surface_area": surface_area,
36
+ "volume": volume,
37
+ "bounding_box": dimensions
38
+ }
static/css/styles.css ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ font-family: Arial, sans-serif;
3
+ margin: 0;
4
+ padding: 0;
5
+ display: flex;
6
+ flex-direction: column;
7
+ align-items: center;
8
+ justify-content: center;
9
+ height: 100vh;
10
+ background-color: #f4f4f9;
11
+ color: #333;
12
+ }
13
+
14
+ h1 {
15
+ margin-bottom: 20px;
16
+ }
17
+
18
+ form {
19
+ display: flex;
20
+ flex-direction: column;
21
+ align-items: center;
22
+ margin-bottom: 20px;
23
+ }
24
+
25
+ input[type="file"] {
26
+ margin-bottom: 10px;
27
+ }
28
+
29
+ button {
30
+ padding: 10px 20px;
31
+ border: none;
32
+ background-color: #0077ff;
33
+ color: white;
34
+ font-size: 16px;
35
+ cursor: pointer;
36
+ border-radius: 5px;
37
+ transition: background-color 0.3s;
38
+ }
39
+
40
+ button:hover {
41
+ background-color: #005bb5;
42
+ }
43
+
44
+ #results {
45
+ margin-top: 20px;
46
+ white-space: pre-wrap;
47
+ text-align: left;
48
+ max-width: 600px;
49
+ overflow-x: auto;
50
+ }
static/js/visualization.js ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.150.1/build/three.module.js';
2
+
3
+ function initScene() {
4
+ const scene = new THREE.Scene();
5
+ const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
6
+ const renderer = new THREE.WebGLRenderer();
7
+ renderer.setSize(window.innerWidth, window.innerHeight);
8
+ document.body.appendChild(renderer.domElement);
9
+
10
+ // Add light
11
+ const light = new THREE.PointLight(0xffffff, 1, 100);
12
+ light.position.set(10, 10, 10);
13
+ scene.add(light);
14
+
15
+ // Add geometry placeholder
16
+ const geometry = new THREE.BoxGeometry();
17
+ const material = new THREE.MeshStandardMaterial({ color: 0x0077ff });
18
+ const cube = new THREE.Mesh(geometry, material);
19
+ scene.add(cube);
20
+
21
+ camera.position.z = 5;
22
+
23
+ function animate() {
24
+ requestAnimationFrame(animate);
25
+ cube.rotation.x += 0.01;
26
+ cube.rotation.y += 0.01;
27
+ renderer.render(scene, camera);
28
+ }
29
+ animate();
30
+ }
31
+
32
+ // Initialize 3D rendering
33
+ window.onload = () => {
34
+ initScene();
35
+ };