Skip to content

Commit

Permalink
MLAB-project/Modules#12 Updated Makefile and added amf sorting.
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKott committed May 22, 2020
1 parent 98a00b7 commit e926f50
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 26 deletions.
99 changes: 73 additions & 26 deletions cad/Makefile
Original file line number Diff line number Diff line change
@@ -1,72 +1,119 @@
AMF_DIR = ../amf
IMG_DIR = ../doc/img
SRC_DIR = ./src
DEP_DIR = ./dep
GCODE_DIR = /Users/jkt/playground/OpenSCAD/kaklik/WINDGAUGE03/amf/gcode
AMF_DIR := ../amf
STL_DIR := ../doc/stl
ASSEMBLY_STL_DIR := $(STL_DIR)
IMG_DIR := ../doc/img
SRC_DIR := ./src
ASSEMBLY_DIR := ./src/assembly
DEP_DIR := ./dep
GCODE_DIR := ../amf/gcode
PLATE_DIMMENSIONS=120
SIMARRANGE=/usr/local/bin/simarrange
STLSORT = stlsort
OPENSCAD_APP = openscad
MERGE_APP = ./merge_modifiers.sh
SLICER_APP = /Users/jkt/playground/OpenSCAD/kaklik/Slic3r/slic3r.pl
VPATH = $(SRC_DIR):$(DEP_DIR):$(AMF_DIR):$(IMG_DIR)
STLSORT := stlsort
AMFSORT := ./amfsort.py
OPENSCAD_APP := $(shell which openscad || which openscad-nightly)
MERGE_APP := ./merge_modifiers.sh
SLICER_APP := slic3r
SLICER_CFG := default.ini
SLICER_PM_CFG := default_prusa_m.ini
#$(GCODE_DIR)/WINDGAUGE_R04.gcode : override SLICER_CFG = default_prusa_m.ini
VPATH := $(SRC_DIR):$(DEP_DIR):$(AMF_DIR):$(IMG_DIR)

SOURCES = $(wildcard $(SRC_DIR)/*.scad)
AMF = $(wildcard $(AMF_DIR)/*.amf)
TARGETS = $(patsubst $(SRC_DIR)/%.scad, $(AMF_DIR)/%.amf, $(SOURCES))
IMAGES = $(patsubst $(SRC_DIR)/%.scad, $(IMG_DIR)/%.png, $(SOURCES))
GCODE = $(patsubst $(AMF_DIR)/%.amf, $(GCODE_DIR)/%.gcode, $(AMF))
STL = $(patsubst $(SRC_DIR)/%.scad, $(AMF_DIR)/%.stl, $(SOURCES))
SOURCES := $(wildcard $(SRC_DIR)/*.scad)
ASSEMBLIES := $(wildcard $(ASSEMBLY_DIR)/*.scad)
AMF := $(wildcard $(AMF_DIR)/*.amf)
STL := $(patsubst $(SRC_DIR)/%.scad, $(STL_DIR)/%.stl, $(SOURCES))
ASSEMBLY := $(patsubst $(ASSEMBLY_DIR)/%.scad, $(ASSEMBLY_STL_DIR)/%.stl, $(ASSEMBLIES))
TARGETS := $(patsubst $(SRC_DIR)/%.scad, $(AMF_DIR)/%.amf, $(SOURCES))
IMAGES := $(patsubst $(SRC_DIR)/%.scad, $(IMG_DIR)/%.png, $(SOURCES))
GCODE := $(patsubst $(AMF_DIR)/%.amf, $(GCODE_DIR)/%.gcode, $(AMF))

all : amf images stl
all : amf gcode stl images

print : amf gcode

view : stl images

calibration:
openscad -m make -o calibration.stl calibration.scad

amf : $(TARGETS)

$(AMF_DIR)/%.amf : $(SRC_DIR)/%.scad
####################################################################################
# MAKING AMF FOR $<
####################################################################################
#
# Update dependencies and make no-draft version of model in AMF.
$(OPENSCAD_APP) -D "draft = false" -m make -o $@ -d $(patsubst $(AMF_DIR)/%.amf, $(DEP_DIR)/%.deps, $@) $<
# Remove absolute paths in dependencies.
sed -i "s|$(shell pwd)/||" $(patsubst $(AMF_DIR)/%.amf, $(DEP_DIR)/%.deps, $@)
# Sort deps file to keep git changes to minimum.
sort -bf $(patsubst $(AMF_DIR)/%.amf, $(DEP_DIR)/%.deps, $@) -o $(patsubst $(AMF_DIR)/%.amf, $(DEP_DIR)/%.deps, $@)
sort -rf $(patsubst $(AMF_DIR)/%.amf, $(DEP_DIR)/%.deps, $@) -o $(patsubst $(AMF_DIR)/%.amf, $(DEP_DIR)/%.deps, $@)
sed -i 's|[^\]$$|& \\|' $(patsubst $(AMF_DIR)/%.amf, $(DEP_DIR)/%.deps, $@)
# Sort AMF file to keep git changes to minimum.
$(AMFSORT) $@
# Add modifiers to AMF files.
$(MERGE_APP) -v -a $@ -s $<
$(MERGE_APP) -a $@ -s $<

include $(wildcard $(DEP_DIR)/*.deps)

stl : $(STL)

$(AMF_DIR)/%.stl : $(SRC_DIR)/%.scad
$(STL_DIR)/%.stl : $(AMF_DIR)/%.amf
####################################################################################
# MAKING STL FOR $<
####################################################################################
#
# Make no-draft version of model in STL.
$(OPENSCAD_APP) -D "draft = false" -m make -o $@ $<
$(OPENSCAD_APP) -D "draft = false" -m make -o $@ $(patsubst $(STL_DIR)/%.stl, $(SRC_DIR)/%.scad, $@)
# Sort stl file to keep git changes to minimum.
$(STLSORT) $@ || :

images : $(IMAGES)

$(IMG_DIR)/%.png : $(AMF_DIR)/%.stl
$(IMG_DIR)/%.png : $(STL_DIR)/%.stl
####################################################################################
# MAKING PNG FOR $<
####################################################################################
#
# Create temporary file for fast image rendering.
-echo "import(\"$<\");" > tmp.scad
echo "import(\"$<\");" > tmp.scad
# Render PNG image from temporary file.
$(OPENSCAD_APP) -o $@ tmp.scad
# Remove temporary file
rm -f tmp.scad

assembly : $(ASSEMBLY)

$(ASSEMBLY_STL_DIR)/%.stl : $(ASSEMBLY_DIR)/%.scad
####################################################################################
# MAKING STL FOR $<
####################################################################################
# $(ASSEMBLY_STL_DIR)
# Update dependencies and make no-draft version of model in STL.
$(OPENSCAD_APP) -D 'stl_dir = "../../../stl/"' -D "draft = false" -m make -o $@ -d $(patsubst $(ASSEMBLY_STL_DIR)/%.stl, $(DEP_DIR)/%.deps, $@) $<
# Remove absolute paths in dependencies.
sed -i "s|$(shell pwd)/||" $(patsubst $(ASSEMBLY_STL_DIR)/%.stl, $(DEP_DIR)/%.deps, $@)
# Sort deps file to keep git changes to minimum.
sort -rf $(patsubst $(ASSEMBLY_STL_DIR)/%.stl, $(DEP_DIR)/%.deps, $@) -o $(patsubst $(ASSEMBLY_STL_DIR)/%.stl, $(DEP_DIR)/%.deps, $@)
sed -i 's|[^\]$$|& \\|' $(patsubst $(ASSEMBLY_STL_DIR)/%.stl, $(DEP_DIR)/%.deps, $@)

gcode : $(GCODE)

$(GCODE_DIR)/%.gcode : $(realpath $(AMF_DIR))/%.amf
$(GCODE_DIR)/%.gcode : $(AMF_DIR)/%.amf
####################################################################################
# MAKING GCODE FOR $<
####################################################################################
#
# Generate gcode files.
$(SLICER_APP) --load default.ini -o $@ --no-gui -j 3 $<
$(SLICER_APP) --load $(GCODE_DIR)/$(SLICER_CFG) -o $@ -j 3 $<
$(SLICER_APP) --load $(GCODE_DIR)/$(SLICER_PM_CFG) -o $(patsubst %.gcode, %_pm.gcode, $@) -j 3 $<

arrange : amf
$(SIMARRANGE) -x $(PLATE_DIMMENSIONS) -y $(PLATE_DIMMENSIONS) $(ARRANGE_TARGETS)

clean:
rm -f calibration.stl
rm -f $(AMF_DIR)/*.amf
rm -f $(AMF_DIR)/*.stl
rm -f $(STL_DIR)/*.stl
rm -f $(IMG_DIR)/*.png
82 changes: 82 additions & 0 deletions cad/amfsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python3

import xml.etree.ElementTree as ET
import sys

# Read command line arguments and check there is only one
arguments = sys.argv
if len(arguments) != 2:
print("Usage: amfprint [FILE]")
sys.exit(2)

amf = arguments[1]

# Read AMF file
try:
tree = ET.parse(amf)
root = tree.getroot()
except ET.ParseError:
print(arguments[0], ": ", amf, ": File not in valid XML format", sep='')
sys.exit(2)
except FileNotFoundError:
print(arguments[0], ": ", amf, ": No such file or directory", sep='')
sys.exit(2)
except:
print("Unexpected error:", sys.exc_info()[0])
sys.exit(2)

# Make list of lists from vertices
vertices = []
for group in root.findall(".//coordinates"):
vertices.append([])
for x in [ x for x in group ]:
vertices[-1].append(float(x.text))

# Make list of lists from volumes
volumes = []
for group in root.findall(".//triangle"):
volumes.append([])
for x in [ x for x in group ]:
volumes[-1].append(int(x.text))

# Test data:
#vertices = [[-54.3898, 38.8428, 6.0], [-54.9202, 38.7432, 6.0], [-54.9202, 34.7432, 6.0],
# [-54.9202, 34.7432, 2.0], [-54.9202, -8.7432, 1.0], [-54.3877, 38.8293, 6.0],
# [-54.9225, 38.7567, 6.0], [-55.1983, 39.1807, 6.0]]
#volumes = [[0, 1, 2], [2, 1, 3], [0, 4, 1], [1, 4, 3],
# [2, 4, 1], [1, 5, 6], [2, 4, 6], [3, 5, 7]]

# Get order of vertices:
# - Enumerate will make (index; value) pairs.
# - Sorted with given key will sort based on values
# - b[0] will return indexes
order = [b[0] for b in sorted(enumerate(vertices), key=lambda i:i[1])]

# Create sorted list of vertices:
sorted_vertices = [ vertices[order[i]] for i in range(len(vertices)) ]

# Creation of sorted volumes as list of lists:
# TODO after volumes are sorted, slic3r returns errors.
sorted_volumes = sorted([[ order.index(x) for x in triangle ] for triangle in volumes ])
#sorted_volumes = [[ order.index(x) for x in triangle ] for triangle in volumes ]

# Change vertices order in AMF file:
i = 0
for group in root.findall(".//coordinates"):
j = 0
for x in [ x for x in group ]:
x.text = str(sorted_vertices[i][j])
j += 1
i += 1

# Change volumes order in AMF file:
i = 0
for group in root.findall(".//triangle"):
j = 0
for x in [ x for x in group ]:
x.text = str(sorted_volumes[i][j])
j += 1
i += 1

# Write back to AMF file
tree.write(amf, encoding="UTF-8", xml_declaration=True)

0 comments on commit e926f50

Please sign in to comment.