DWGtoPNGconverter/start

301 lines
9.2 KiB
Bash
Executable File

#!/bin/sh
clear
Outerwallsflag="false"
PPI=600
THRESHOLD=0.01
buildingcount=1
SCALE=1000
FILES=$PWD/
binary="/Applications/QCAD-Pro.app/Contents/resources/qcad"
shopt -s nullglob
taskCount=(*.dwg)
taskCount=${#taskCount[@]}
taskDone=0
progressBarWidth=20
trap ctrl_c INT
ctrl_c(){
cleanup
exit 1
}
mainConversion (){
convert -density $2 \
"Helperfiles/$1.pdf" \
-alpha off \
-negate \
\( \
-clone 0 \
-fill "#000000" \
-colorize 100 \
-colorize 100 \
\) \
\( \
-clone 0,1 \
-compose difference \
-composite \
-separate \
+channel \
-evaluate-sequence max \
-auto-level \
\) \
-delete 1 \
-alpha off \
-compose over \
-compose copy_opacity \
-composite \
-resize 2048x2048 \
-sharpen 0x1.0 \
-colorspace Gray \
-quality 90 \
"PNG/$1.png"
convert "PNG/$1.png" \
-channel A -fx "p*(p>$3?22:0)"\
"PNG/$1.png"
convert -density $2 \
"Helperfiles/$1.pdf" \
-resize 2048x2048 \
-sharpen 0x1.0 \
-colorspace Gray \
-quality 90 \
-background white \
-alpha remove \
-negate \
-negate \
"PRIMEPNG/$1.png"
}
getCoordinates (){
convert -density "$1" \
"Coordinatefiles/$2.pdf" \
-resize 2048x2048 \
-sharpen 0x1.0 \
-colorspace Gray \
-quality 90 \
-background white \
-alpha remove \
-negate \
-threshold 0 \
-negate \
"Coordinatefiles/auto-level.png"
convert "Coordinatefiles/auto-level.png" -fill red -fuzz 5% -draw "color 0,0 floodfill" -fill black +opaque red -fill white -opaque red -alpha off -morphology close diamond "Coordinatefiles/filled.png"
convert "Coordinatefiles/filled.png" -morphology edgein octagon:1 "Coordinatefiles/finallines.png"
convert "Coordinatefiles/finallines.png" -transparent black "Coordinatefiles/finaledges.png"
convert "Coordinatefiles/finaledges.png" txt: > "Coordinatefiles/test1.txt"
grep -v '(0,0,0,0)' "Coordinatefiles/test1.txt" > "Coordinatefiles/test2.txt"
grep -v 'graya(255,0)' "Coordinatefiles/test2.txt" > "Coordinatefiles/test3.txt"
awk '{print $1}' "Coordinatefiles/test3.txt" > "Coordinatefiles/test4.txt"
cat "Coordinatefiles/test4.txt" | grep -vE '#' | sed 's/://g' > "Coordinatefiles/$cleanfile.csv"
}
progressBar () {
RED='\033[1;32m'
NC='\033[0m'
progress=$(echo "$progressBarWidth/$taskCount*$tasksDone" | bc -l)
fill=$(printf "%.0f\n" $progress)
if [ $fill -gt $progressBarWidth ]; then
fill=$progressBarWidth
fi
empty=$(($fill-$progressBarWidth))
percent=$(echo "100/$taskCount*$tasksDone" | bc -l)
percent=$(printf "%0.2f\n" $percent)
if [ $(echo "$percent>100" | bc) -gt 0 ]; then
percent="100.00"
fi
printf "\r["
printf "${RED}%${fill}s" '' | tr ' '
printf "${NC}%${empty}s" '' | tr ' '
printf "] $percent%% - $text "
}
createHelperFile(){
file="Helperfile.txt"
touch $file
echo "--Command to run--\n" > $file
echo "./start -x $XOFF -y $YOFF -w $WIDTH -h $HEIGHT -s $SCALE -p $PPI -t $THRESHOLD -b $buildingcount" >> $file
echo "\n--Variables-- \n" >> $file
echo "-xoffset: $XOFF" >> $file
echo "-yoffset: $YOFF" >> $file
echo "-width: $WIDTH" >> $file
echo "-height: $HEIGHT" >> $file
echo "-scale: $SCALE" >> $file
echo "-PPI: $PPI" >> $file
echo "-Threshold $THRESHOLD" >> $file
echo "-buildingcount $buildingcount" >> $file
layer="./layers.txt"
echo "\n--Active layer list--\n" >> $file
awk '!a[$0]++' "layers.txt" >>$file
}
createDirectories(){
rm -f "Coordinatefiles/minified.json"
if [[ ! -e "$DWGfiles" ]]; then
mkdir -p DWGfiles
fi
if [[ ! -e "$PNG" ]]; then
mkdir -p PNG
fi
if [[ ! -e "$Helperfiles" ]]; then
mkdir -p Helperfiles
fi
if [[ ! -e "$PRIMEPNG" ]]; then
mkdir -p PRIMEPNG
fi
if [[ ! -e "Coordinatefiles" ]]; then
mkdir -p Coordinatefiles
fi
}
removeCapitalLetters(){
for f in "$FILES"*.DWG
do
pdffile=${f##*/}
cleanfile=${pdffile%.*}
mv -f "$f" "$cleanfile.dwg"
done
for f in "$FILES"*.dwg
do
mv -f "$f" "${f// /_}"
done
}
readHelperfile(){
if [[ -e "Helperfile.txt" ]]; then
command=$(grep "./start" "./Helperfile.txt")
commandarray=($command)
if [[ -z ${XOFF+x} ]]; then XOFF=${commandarray[2]}; fi
if [[ -z ${YOFF+x} ]]; then YOFF=${commandarray[4]}; fi
if [[ -z ${WIDTH+x} ]]; then WIDTH=${commandarray[6]}; fi
if [[ -z ${HEIGHT+x} ]]; then HEIGHT=${commandarray[8]}; fi
if [[ -z ${SCALE+x} ]]; then SCALE=${commandarray[10]}; fi
if [[ -z ${PPI+x} ]]; then PPI=${commandarray[12]}; fi
if [[ -z ${THRESHOLD+x} ]]; then THRESHOLD=${commandarray[14]}; fi
if [[ -z ${buildingcount+x} ]]; then buildingcount=${commandarray[16]}; fi
fi
}
createJSONFiles(){
if [ "$Outerwallsflag" == "true" ];then
pngs="$FILES""PNG/"*.png
arr=($pngs)
width=$(identify -format '%w' "${arr[0]}")
height=$(identify -format '%h' "${arr[0]}")
python3 lines.py "$buildingcount" "$width" "$height"
Coordinatefiles=$PWD/Coordinatefiles/
array=()
for c in $Coordinatefiles*.json
do
array+=("$c")
done
jq -s add "${array[@]}" > "Coordinatefiles/final.json"
jq -c . < "Coordinatefiles/final.json" > "Coordinatefiles/minified.json"
fi
}
checkMainArguments(){
if [ ! -z ${XOFF+x} ]; then XOFF=$XOFF; fi
if [ ! -z ${YOFF+x} ]; then YOFF=$YOFF; fi
if [ ! -z ${WIDTH+x} ]; then WIDTH=$WIDTH; fi
if [ ! -z ${HEIGHT+x} ]; then HEIGHT=$HEIGHT; fi
if [ ! -z ${SCALE+x} ]; then SCALE=$SCALE; fi
if [ ! -z ${THRESHOLD+x} ]; then THRESHOLD=$THRESHOLD; fi
if [ ! -z ${PPI+x} ]; then PPI=$PPI; fi
if [ ! -z ${Outerwallsflag+x} ]; then Outerwallsflag=$Outerwallsflag; fi
if [ ! -z ${buildingcount+x} ]; then buildingcount=$buildingcount; fi
}
moveDWGFiles(){
for f in "$FILES"*.dwg
do
mv "$f" "DWGfiles"
done
}
checkHelperFile(){
allparam=("$@")
if [[ ${#allparam[@]} -le 9 ]]; then
if [[ ! -e "Helperfile.txt" ]]; then
echo "No helperfile found in the current directory. Please fill in all parameters to run this script for the first time. A Helperfile will be created afterwards."
exit -1
fi
fi
}
runGetOpts(){
local OPTIND=1
while getopts "x:y:w:h:s:p:t:b: o" option
do
case "${option}"
in
x) XOFF=$OPTARG;;
y) YOFF=$OPTARG;;
w) WIDTH=$OPTARG;;
h) HEIGHT=$OPTARG;;
s) SCALE=$OPTARG;;
t) THRESHOLD=$OPTARG;;
p) PPI=$OPTARG;;
o) Outerwallsflag="true";;
b) buildingcount=$OPTARG;;
?) echo "Something went wrong. Remember that x,y,w,h,s need to be followed by some value." && exit 1;;
esac
done
}
printToBeConverted(){
echo "\n\nStarting script with\n\
|| -x $XOFF || -y $YOFF || -w $WIDTH || -h $HEIGHT || -s $SCALE || \n\
|| -p $PPI || -t $THRESHOLD || \n\
|| -b $buildingcount || -o $Outerwallsflag ||\
\nMore details can be found in the Helperfile.\
\n...please be patient..."
}
cleanup(){
rm -f "Coordinatefiles/finaledges.png" "Coordinatefiles/final.json" "Coordinatefiles/filled.png" "Coordinatefiles/finallines.png" "Coordinatefiles/auto-level.png" "Coordinatefiles/final.png" "Coordinatefiles/5.png" "Coordinatefiles/test1.txt" "Coordinatefiles/test2.txt" "Coordinatefiles/test3.txt" "Coordinatefiles/test4.txt" "Coordinatefiles/1.png" "Coordinatefiles/2.png" "Coordinatefiles/4.png"
echo "\n...should be all done..."
#Just to remove the annoying debug log that QCAD automatically makes. This keeps the terminal screen clean as well.
log="./-x"
[ -e "$log" ] && rm $log
}
checkHelperFile "$@"
runGetOpts "$@"
checkMainArguments
readHelperfile
createDirectories
createHelperFile
removeCapitalLetters
printToBeConverted
for f in "$FILES"*.dwg
do
pdffile=${f##*/}
cleanfile=${pdffile%.*}
text=$(echo "$cleanfile done")
"$binary" -no-gui -no-dock-icon -allow-multiple-instances -autostart "javascript.js" "-f" "$f" "-x" "$XOFF" "-y" "$YOFF" "-w" "$WIDTH" "-h" "$HEIGHT" "-s" "$SCALE" "-o" "$Outerwallsflag" "-r" 2>"-x" | grep "MESSAGE:"
mainConversion $cleanfile $PPI $THRESHOLD
if [ "$Outerwallsflag" == "true" ];then
getCoordinates $PPI $cleanfile
fi
(( tasksDone += 1 ))
progressBar $taskCount $taskDone $text
done
createJSONFiles
moveDWGFiles
cleanup