Skip to content

Commit

Permalink
SUKU preview added for mechanical
Browse files Browse the repository at this point in the history
  • Loading branch information
SukuWc committed Jul 3, 2024
1 parent c3e872c commit 628af69
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 0 deletions.
167 changes: 167 additions & 0 deletions .github/workflows/freecad_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import FreeCAD
import FreeCADGui
import TechDrawGui

import PartGui
import PartDesignGui
import ImportGui

import sys

print(f"sys.argv = {sys.argv}")

import os
if os.path.isdir("temp"):
print("Directory already exists")
else:
os.mkdir("temp")

export_list = sys.argv[3:]

print(f"export_list = {export_list}")

if "stl" in export_list:
print("export stl requested")
if "step" in export_list:
print("export step requested")
if "pdf" in export_list:
print("export pdf requested")

docname = sys.argv[2]
print("Opening document: ", docname, os.path.isfile(docname))
App.openDocument(docname)

def hideAll():
doc = App.ActiveDocument
assert(doc)
bodies = [obj for obj in doc.Objects if obj.isDerivedFrom("PartDesign::Body")]
shown = []
for body in bodies:
grp = body.Group
# print (body.Label)
App.Gui.ActiveDocument.getObject(body.Name).hide()

def showPartDesignBody(name):
doc = App.ActiveDocument
assert(doc)
bodies = [obj for obj in doc.Objects if obj.isDerivedFrom("PartDesign::Body")]
shown = []
hideAll()
for body in bodies:
if body.Label == name:
grp = body.Group
#print (body.Label)
App.Gui.ActiveDocument.getObject(body.Name).show()
for feat in body.Origin.OriginFeatures:
if feat.Label in shown:
continue
shown.append(feat.Label)

for item in grp:
if item.TypeId == "Sketcher::SketchObject":
continue

#print(f"-->{item.TypeId} {item.Label}")
App.Gui.ActiveDocument.getObject(item.Name).show()

def exportScreenshot(label, filename):
hideAll()
showPartDesignBody(label)

view = FreeCADGui.ActiveDocument.ActiveView
view.viewAxometric()
print("STEP 3",filename)
view.fitAll()
print("STEP 4",filename)
FreeCADGui.updateGui()
print("STEP 5",filename)

print("SCREENSHOT", label, filename)
App.Gui.ActiveDocument.ActiveView.saveImage(filename,3200,2400,'Transparent')

hideAll()

objs = App.ActiveDocument.Objects

# ========= EXPORT App:Part to STEP =========
for obj in objs:
sono=App.ActiveDocument.getObject(obj.Name)
# sono.ViewObject.show()

# Ez kell ide
FreeCADGui.updateGui()
FreeCADGui.updateGui()
FreeCADGui.updateGui()
FreeCADGui.updateGui()
FreeCADGui.updateGui()
FreeCADGui.updateGui()

if sono.TypeId == "App::Part":
print(obj.Label, obj.Name, "STEP")
sono.Shape.exportStep("temp/"+obj.Label+".step")
#__objs__=[]
#__objs__.append(App.ActiveDocument.getObject(obj.Name))
#print(__objs__)
#import ImportGui
#ImportGui.export(__objs__,"temp/"+obj.Label+"2.step")

# sono.ViewObject.hide()


hideAll()

# ========= EXPORT PartDesign:Body to STEP, STL, PNG, PDF =========

for obj in objs:
sono=App.ActiveDocument.getObject(obj.Name)
sono.ViewObject.show()

# Ez kell ide
FreeCADGui.updateGui()
FreeCADGui.updateGui()
FreeCADGui.updateGui()
FreeCADGui.updateGui()
FreeCADGui.updateGui()
FreeCADGui.updateGui()

if sono.TypeId == "PartDesign::Body":

if "png" in export_list:
print(obj.Label, obj.Name, "PNG")
exportScreenshot(obj.Label, "temp/"+obj.Label+".png")

if "step" in export_list:
print(obj.Label, obj.Name, "STEP")

sono.Shape.exportStep("temp/"+obj.Label+".step")

if "stl" in export_list:
print(obj.Label, obj.Name, "STL")
sono.Shape.exportStl("temp/"+obj.Label+".stl")

elif sono.TypeId == "TechDraw::DrawPage":
if "pdf" in export_list:
print(obj.Label, obj.Name, "DRAW")
TechDrawGui.export([sono],u"temp/"+obj.Label+".pdf")

sono.ViewObject.hide()


hideAll()




print("DONE")

App.Gui.SendMsgToActiveView("Save")
print("DONE2")

App.ActiveDocument.save()
print("DONE3")


App.Gui.getMainWindow().close()

# sys.exit(0)
# exit(0)
119 changes: 119 additions & 0 deletions .github/workflows/mechanical_components.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
name: mechanical_components

on:
workflow_dispatch:
push:
paths:
- '.github/workflows/mechanical_components.yml'
- '.github/workflows/freecad_export.py'
- '.github/workflows/openscad_preview.sh'
- 'Mechanical/Design/**'

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
generate-artifacts:
name: freecad export
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
component:
- 'KNOT-ASSEMBLY'

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y xvfb x11vnc
sudo apt-get install snapd
sudo snap install freecad
sudo apt-get install -y openscad
Xvfb :99 -screen 0 1920x1080x24 > /dev/null 2>&1 &
sleep 10 # Give Xvfb some time to start
x11vnc -display :99 -nopw -listen localhost -xkb -ncache 10 -ncache_cr -forever > /dev/null 2>&1 &
- name: Run GUI application
continue-on-error: true
run: |
ls
export DISPLAY=:99
timeout 1m freecad --hidden ./Mechanical/Design/${{ matrix.component }}/${{ matrix.component }}.FCStd ./.github/workflows/freecad_export.py step pdf
- name: Install conda, pythonocc and do the conversion
run: |
CONDA_INSTALL_DIR="$HOME/miniconda3"
# Download and install Miniconda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
bash miniconda.sh -b -p "$CONDA_INSTALL_DIR"
rm miniconda.sh
# Add Miniconda to PATH
export PATH="$CONDA_INSTALL_DIR/bin:$PATH"
# Initialize conda
conda init bash
# Print Miniconda version
conda --version
conda install -c conda-forge pythonocc-core=7.7.2 --yes
conda --version
python ./.github/workflows/pythonocc_step_to_stl.py
- name: Generate Preview PNG
run: |
./.github/workflows/openscad_preview.sh
- name: Stop x11vnc
run: pkill x11vnc

- name: Set Date
run: echo "action_date=$(date +'%Y-%m-%d-%H%M')" >> $GITHUB_ENV

- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: "result_${{ matrix.component }}_${{ env.action_date }}"
path: temp/*.*

deploy-results:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master'
needs: generate-artifacts
steps:
- uses: actions/checkout@v2
with:
ref: preview

- name: Download reports' artifacts
uses: actions/download-artifact@v4
with:
path: download

- name: Collecting files
run: |
# Create the temp folder if it doesn't exist
mkdir -p Preview/Mechanical
# Loop through each subdirectory in the download folder
for subdirectory in download/*; do
# Check if it is a directory
if [ -d "$subdirectory" ]; then
# Copy the contents of the subdirectory to the temp folder
cp -r "$subdirectory"/* "Preview/Mechanical/"
fi
done
- name: Commit the files
run: |
git config --global user.name ${{ github.actor }}
git config --global user.email ${{ github.actor }}@users.noreply.github.com
git add Preview/*
git commit -m "BOT ${{ github.workflow }} ${{ github.sha }}"
git push
24 changes: 24 additions & 0 deletions .github/workflows/pythonocc_step_to_stl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
from OCC.Extend.DataExchange import *

directory_path = './temp' # Replace with the path to your directory

# List all files in the directory
file_list = [f for f in os.listdir(directory_path) if f.endswith('.step')]

# Process each .step file
for step_file in file_list:
step_file_path = os.path.join(directory_path, step_file)
print(f"Converting {step_file} to STL...")

# Read STEP file
shape = read_step_file(step_file_path)

# Generate STL file name (replace .step with .stl)
stl_file = os.path.splitext(step_file)[0] + '.stl'
stl_file_path = os.path.join(directory_path, stl_file)

# Write STL file
write_stl_file(shape, stl_file_path, mode='binary', linear_deflection=0.1, angular_deflection=0.5)

print(f"Conversion of {step_file} to {stl_file} complete\n")
Binary file removed Mechanical/KNOT-ASSEMBLY.20240625-093937.FCBak
Binary file not shown.
File renamed without changes.

0 comments on commit 628af69

Please sign in to comment.