forked from AllwineDesigns/stl_cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e204419
Showing
7 changed files
with
1,903 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
all: bin/stl_header bin/stl_merge bin/stl_transform | ||
|
||
bin/stl_header: src/stl_header.c src/stl_util.h | ||
gcc src/stl_header.c -o bin/stl_header | ||
|
||
bin/stl_merge: src/stl_merge.c src/stl_util.h | ||
gcc src/stl_merge.c -o bin/stl_merge | ||
|
||
bin/stl_transform: src/stl_transform.c src/stl_util.h | ||
gcc src/stl_transform.c -o bin/stl_transform | ||
|
||
clean: | ||
rm -rf bin/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
stl_cmd | ||
======= | ||
|
||
The goal of each stl_cmd is to provide a simple command line interface for | ||
manipulating binary STL files. They each aim to be fast and to have no dependencies. | ||
|
||
Copyright 2014 Freakin' Sweet Apps, LLC ([email protected]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
Copyright 2014 by Freakin' Sweet Apps, LLC ([email protected]) | ||
This file is part of stl_cmd. | ||
stl_cmd 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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "stl_util.h" | ||
|
||
#define BUFFER_SIZE 4096 | ||
|
||
void print_usage() { | ||
fprintf(stderr, "usage: stl_header [-s <header>] [-o <output file>] <input file>\n"); | ||
fprintf(stderr, " If both -s and -o flags are specified <input file> is copied to <output\n"); | ||
fprintf(stderr, " file> and its header is set to <header>. If -o is not specified and -s is\n"); | ||
fprintf(stderr, " then the header is set on the input file. If neither -o nor -s is specified,\n"); | ||
fprintf(stderr, " the header of the model is output to the terminal.\n"); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
int c; | ||
int errflg = 0; | ||
char *set_header; | ||
char *out_file; | ||
int setflag = 0; | ||
int outflag = 0; | ||
|
||
while((c = getopt(argc, argv, "s:o:")) != -1) { | ||
switch(c) { | ||
case 'o': | ||
outflag = 1; | ||
out_file = optarg; | ||
break; | ||
case 's': | ||
setflag = 1; | ||
set_header = optarg; | ||
break; | ||
case '?': | ||
fprintf(stderr, "Unrecognized option: '-%c'\n", optopt); | ||
errflg++; | ||
break; | ||
} | ||
} | ||
|
||
if(errflg || optind >= argc) { | ||
print_usage(); | ||
exit(2); | ||
} | ||
|
||
char* filename = argv[optind]; | ||
char header[80]; | ||
|
||
if(!is_valid_binary_stl(filename)) { | ||
fprintf(stderr, "stl_header only accepts binary stl files.\n"); | ||
exit(2); | ||
} | ||
|
||
FILE *f; | ||
f = fopen(filename, "r+b"); | ||
if(!f) { | ||
fprintf(stderr, "Can't read file: %s\n", filename); | ||
exit(2); | ||
} | ||
|
||
if(setflag && outflag) { | ||
strncpy(header, set_header, 80); | ||
FILE *outf; | ||
outf = fopen(out_file, "wb"); | ||
if(!outf) { | ||
fprintf(stderr, "Can't write to file: %s\n", out_file); | ||
exit(2); | ||
} | ||
fwrite(header, 80, 1, outf); | ||
fseek(f, 80, SEEK_SET); | ||
|
||
char buffer[BUFFER_SIZE]; | ||
|
||
int r; | ||
while((r = fread(buffer, 1, BUFFER_SIZE, f))) { | ||
fwrite(buffer, 1, r, outf); | ||
} | ||
fclose(f); | ||
fclose(outf); | ||
} else if(setflag) { | ||
strncpy(header, set_header, 80); | ||
fwrite(header, 80, 1, f); | ||
fclose(f); | ||
} else { | ||
fread(header, 80, 1, f); | ||
fclose(f); | ||
|
||
for(int i = 0; i < 80; i++) { | ||
printf("%c", header[i]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
Copyright 2014 by Freakin' Sweet Apps, LLC ([email protected]) | ||
This file is part of stl_cmd. | ||
stl_cmd 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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
#include <unistd.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <libgen.h> | ||
#include <string.h> | ||
#include "stl_util.h" | ||
|
||
#define BUFFER_SIZE 4096 | ||
|
||
void print_usage() { | ||
fprintf(stderr, "usage: stl_merge -o <out file> <in file1> <in file2>\n"); | ||
fprintf(stderr, " Merges two binary stl files into a single file."); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
int c; | ||
int errflg = 0; | ||
char *out_file; | ||
int outflag = 0; | ||
|
||
while((c = getopt(argc, argv, "o:")) != -1) { | ||
switch(c) { | ||
case 'o': | ||
outflag = 1; | ||
out_file = optarg; | ||
break; | ||
case '?': | ||
fprintf(stderr, "Unrecognized option: '-%c'\n", optopt); | ||
errflg++; | ||
break; | ||
} | ||
} | ||
|
||
if(errflg || !outflag || argc != 5) { | ||
print_usage(); | ||
exit(2); | ||
} | ||
|
||
char* file1 = argv[optind]; | ||
char* file2 = argv[optind+1]; | ||
|
||
if(!is_valid_binary_stl(file1)) { | ||
fprintf(stderr, "file1 is not a binary stl file.\n"); | ||
exit(2); | ||
} | ||
|
||
if(!is_valid_binary_stl(file2)) { | ||
fprintf(stderr, "file2 is not a binary stl file.\n"); | ||
exit(2); | ||
} | ||
|
||
FILE *f1; | ||
FILE *f2; | ||
FILE *outf; | ||
|
||
f1 = fopen(file1, "rb"); | ||
if(!f1) { | ||
fprintf(stderr, "Can't read file1: %s\n", file1); | ||
exit(2); | ||
} | ||
|
||
f2 = fopen(file2, "rb"); | ||
if(!f2) { | ||
fprintf(stderr, "Can't read file2: %s\n", file2); | ||
exit(2); | ||
} | ||
|
||
outf = fopen(out_file, "wb"); | ||
if(!outf) { | ||
fprintf(stderr, "Can't open out file: %s\n", out_file); | ||
exit(2); | ||
} | ||
|
||
fseek(f1, 80, SEEK_SET); | ||
fseek(f2, 80, SEEK_SET); | ||
|
||
uint32_t num_tris1; | ||
uint32_t num_tris2; | ||
|
||
fread(&num_tris1, 4, 1, f1); | ||
fread(&num_tris2, 4, 1, f2); | ||
|
||
char header[81] = {0}; // include an extra char for terminating \0 of snprintf | ||
char base1[50]; | ||
char base2[50]; | ||
strncpy(base1, basename(file1), 50); | ||
strncpy(base2, basename(file2), 50); | ||
snprintf(header, 81, "Merged copy of %s and %s", base1, base2); | ||
|
||
uint32_t merged_tris = num_tris1+num_tris2; | ||
|
||
fwrite(header, 80, 1, outf); | ||
fwrite(&merged_tris, 4, 1, outf); | ||
|
||
char buffer[BUFFER_SIZE]; | ||
|
||
int r; | ||
while((r = fread(buffer, 1, BUFFER_SIZE, f1))) { | ||
fwrite(buffer, 1, r, outf); | ||
} | ||
fclose(f1); | ||
|
||
while((r = fread(buffer, 1, BUFFER_SIZE, f2))) { | ||
fwrite(buffer, 1, r, outf); | ||
} | ||
fclose(f2); | ||
fclose(outf); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.