ホームに戻る
DXF ファイル を JAVA3D のコードに変換
0、使い方
自分のために作ったコードです。
内部のエラー処理がいい加減です。
コンパイラは Borland C++ Compiler を使用。
Shade で作ったモデルを JAVA3D のコードに変換します。
モデルは掃引などを使用していないものを使います。
1個のモデルを3角形で分割したものを使用。
Shade から DXF 形式を選択して書き出します。
コードはCで書いてあります。
2段階変換です。
コマンドラインで以下のようになります。
dxf2txt.exe ***.dxf > ***.txt
txt2java.exe *** xxx.txt > ***.java
1、コード1
/*
DXF 形式から必要な部分を抽出
*/
#include <stdio.h>
static int point_count = 0;
static int triangle_count = 0;
/*
s1 と s2 が同じであれば 1 を返す なければ 0 を返す
*/
int compare(char *s1, char *s2){
int i;
for(i = 0;; i++){
if(s1[i] == '\0' && s2[i] == '\0'){
return 1;
}
if(s1[i] - s2[i] != 0){
return 0;
}
}
}
/*
dxf ファイルから必要部分を抽出
*/
void read_file(char *filename){
int c;
int flag = 0;
int index = 0;
char buf[256];
FILE *fp;
fp = fopen(filename, "r");
while(1){
c = fgetc(fp);
if(c == EOF){
break;
}
if(c == '\n'){
if(flag == 1){
flag = 0;
printf("\n");
}
buf[index] = '\0';
if(compare(buf, " 10") == 1){
flag = 1;
}
if(compare(buf, " 20") == 1){
flag = 1;
}
if(compare(buf, " 30") == 1){
point_count++;
flag = 1;
}
if(compare(buf, " 71") == 1){
if(point_count != 0){
flag = 1;
}
}
if(compare(buf, " 72") == 1){
flag = 1;
}
if(compare(buf, " 73") == 1){
triangle_count++;
flag = 1;
}
index = 0;
continue;
}
buf[index] = (char)c;
if(flag == 1){
putchar(c);
}
index++;
}
fclose(fp);
}
int main(int argc,char *argv[]){
if(argc != 2){
printf("convert ***.dxf to ***.txt\n", argv[0]);
printf("%s ***.dxf > test.txt\n", argv[0]);
return -1;
}
read_file(argv[1]);
printf("p=%d\n", point_count);
printf("t=%d\n", triangle_count);
return 0;
}
2、コード2
/*
DXF からのテキストを JAVA のコードに変換
*/
#include <stdio.h>
static int point_count;
static int triangle_count;
static char class_name[256];
void print_header(){
printf("import javax.media.j3d.*;\n");
printf("import javax.vecmath.*;\n\n");
printf("public class %s extends TriangleArray{\n\n", class_name);
printf(" public %s(){\n", class_name);
printf(" super(%d, TriangleArray.COORDINATES | TriangleArray.NORMALS);\n\n", triangle_count * 3);
printf(" Point3d[] verteces = new Point3d[%d];\n", point_count);
printf(" Point3d[] verteces2 = new Point3d[%d];\n\n", triangle_count * 3);
}
void print_footer(){
printf("\n for(int i = 0; i < %d; i++){\n", triangle_count * 3);
printf(" verteces2[i] = verteces[indeces[i]];\n");
printf(" }\n\n");
printf(" this.setCoordinates(0, verteces2);\n\n");
printf(" Vector3f[] normals = calcFaceNormals(verteces2);\n\n");
printf(" this.setNormals(0, normals);\n");
printf(" }\n\n");
printf(" private Vector3f[] calcFaceNormals(Point3d[] vertices) {\n");
printf(" Vector3f[] normals = new Vector3f[vertices.length];\n\n");
printf(" for(int i=0; i<vertices.length; i+=3) {\n");
printf(" Vector3f fnormal = calcFaceNormal(i, vertices);\n");
printf(" normals[i] = fnormal;\n");
printf(" normals[i + 1] = fnormal;\n");
printf(" normals[i + 2] = fnormal;\n");
printf(" }\n\n");
printf(" return normals;\n");
printf(" }\n\n");
printf(" private Vector3f calcFaceNormal(int index, Point3d[] vertices){\n");
printf(" double ax = vertices[index + 2].x - vertices[index + 1].x;\n");
printf(" double ay = vertices[index + 2].y - vertices[index + 1].y;\n");
printf(" double az = vertices[index + 2].z - vertices[index + 1].z;\n");
printf(" double bx = vertices[index + 0].x - vertices[index + 1].x;\n");
printf(" double by = vertices[index + 0].y - vertices[index + 1].y;\n");
printf(" double bz = vertices[index + 0].z - vertices[index + 1].z;\n\n");
printf(" double nx = ay * bz - az * by;\n");
printf(" double ny = az * bx - ax * bz;\n");
printf(" double nz = ax * by - ay * bx;\n\n");
printf(" Vector3f normal = new Vector3f((float)nx, (float)ny, (float)nz);\n");
printf(" normal.normalize();\n\n");
printf(" return normal;\n");
printf(" }\n");
printf("}\n");
}
void read_file(char *filename){
char c;
char buf[256];
int index = 0;
FILE *fp;
int point_index = 0;
int triangle_index = 0;
int index_index = 0;
fp = fopen(filename, "r");
while(1){
c = fgetc(fp);
if(c == EOF){
break;
}
if(c == '\n'){
buf[index] = '\0';
if(buf[0] == 'p'){
sscanf(buf, "p=%d", &point_count);
}
if(buf[0] == 't'){
sscanf(buf, "t=%d", &triangle_count);
}
index = 0;
continue;
}
buf[index] = (char)c;
index++;
}
print_header();
fseek(fp, 0L, SEEK_SET);
index = 0;
while(1){
c = fgetc(fp);
if(c == EOF){
break;
}
if(c == '\n'){
buf[index] = '\0';
if(triangle_index == 0){
printf(" verteces[%d] = new Point3d(%s", point_index, buf);
}
else{
printf(",%s", buf);
}
triangle_index++;
if(triangle_index == 3){
printf(");\n");
triangle_index = 0;
point_index++;
if(point_index == point_count){
break;
}
}
index = 0;
continue;
}
buf[index] = (char)c;
index++;
}
index = 0;
while(1){
c = fgetc(fp);
if(c == EOF){
break;
}
if(c == '\n'){
int num;
buf[index] = '\0';
sscanf(buf, "%d", &num);
if(index_index == 0){
printf("\n int[] indeces = {\n %d", num - 1);
}
else{
printf(",%d", num - 1);
}
index_index++;
index = 0;
continue;
}
buf[index] = (char)c;
index++;
}
printf("\n };\n");
fclose(fp);
print_footer();
}
int main(int argc,char *argv[]){
int i;
for(i = 0; argv[1][i] != '\0'; i++){
class_name[i] = argv[1][i];
}
class_name[i] = '\0';
if(argc != 3){
printf("convert xxx.txt to ***.java\n", argv[0]);
printf("%s *** xxx.txt > ***.java\n", argv[0]);
return -1;
}
read_file(argv[2]);
return 0;
}