<commit_before>/* $RCSfile: $
 * $Revision: $ $Date: $
 * Auth: <NAME> (<EMAIL>)
 * 
 * Copyright (c) 1991-2015 by STEP Tools Inc. 
 * All Rights Reserved.
 * 
 * Permission to use, copy, modify, and distribute this software and
 * its documentation is hereby granted, provided that this copyright
 * notice and license appear on all copies of the software.
 * 
 * STEP TOOLS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. STEP TOOLS
 * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
 * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

#include <stp_schema.h>
#include <stix.h>
#include <stixmesh.h>

#include "stp2webgl.h"

// write_stl() -- write a single STL file for a STEP model.  This
// facets everything in one pass, and then work on the cached data.
// It then recursively walks down through any assemblies, applying
// transforms to the facet data and writing ASCII STL.
//

extern void facet_all_products (stp2webgl_opts * opts);
extern int write_stl (stp2webgl_opts * opts);

static void print_mesh_for_product (
    FILE * stlfile,
    stp_product_definition * pd,
    StixMtrx &starting_placement
    );

// ======================================================================

extern int write_stl (stp2webgl_opts * opts)
{    
    FILE * stlfile = stdout;
    unsigned i,sz;
    
    if (opts->do_split)
    {
	printf ("Only single STL file output currently implemented\n");
	return 2;
    }
    
    if (opts->dstfile)
    {
	stlfile = rose_fopen(opts->dstfile, "w");
	if (!stlfile) {
	    printf ("Could not open output file\n");
	    return 2;
	}
    }

    // Recursively facet all of the products in the root assemblies
    // and attach each resulting mesh to the representation item for
    // each solid.
    //
    facet_all_products(opts);

    
    fputs ("solid ", stlfile);
    if (opts-> dstfile) fputs (opts-> dstfile, stlfile);
    fputs ("\n", stlfile);

    // Now print the mesh details along with placement info
    for (i=0, sz=opts->root_prods.size(); i<sz; i++)
    {
	// The root placement is usually the identity matrix but some
	// systems put a standalone AP3D at the top to place the whole
	// thing in the global space.
	StixMtrx root_placement; 

	print_mesh_for_product (stlfile, opts->root_prods[i], root_placement);
    }

    fputs ("endsolid ", stlfile);
    if (opts-> dstfile) fputs (opts-> dstfile, stlfile);
    fputs ("\n", stlfile);

    return 0;
}



//------------------------------------------------------------
//------------------------------------------------------------
// PRINT THE FACET INFORMATION -- This follows the shape information
// attached to a single product or assembly and prints it to the STL
// file.  This is adapted from the stixmesh facet assembly sample.
//
// Since the shapes are in a tree that parallels the product tree, we
// look for attached next_assembly_usage_occurrences (NAUO) that tell
// us when we are moving into the shape of another product.
//------------------------------------------------------------
//------------------------------------------------------------



void print_triangle (
    FILE * stlfile,
    const StixMeshFacetSet * fs,
    StixMtrx &xform,
    unsigned facet_num
    )
{
    double v[3];
    double n[3];
    const StixMeshFacet * f = fs-> getFacet(facet_num);
    const char * vertexfmt = "        vertex %.15g %.15g %.15g\n";

    if (!f) return;

    // The components of the triangle verticies and vertex normals are
    // given by an index into internal tables.  Apply the transform so
    // that the facet is placed correctly in the part space.
    //
    fs->getFacetNormal(n, f);
    stixmesh_transform_dir (n, xform, n); 
    fprintf(stlfile, "facet normal %.15g %.15g %.15g\n", n[0], n[1], n[2]);
    
    fputs("    outer loop\n", stlfile);

    stixmesh_transform (v, xform, fs-> getVertex(f-> verts[0]));
    fprintf(stlfile, vertexfmt, v[0], v[1], v[2]);

    stixmesh_transform (v, xform, fs-> getVertex(f-> verts[1]));
    fprintf(stlfile, vertexfmt, v[0], v[1], v[2]);

    stixmesh_transform (v, xform, fs-> getVertex(f-> verts[2]));
    fprintf(stlfile, vertexfmt, v[0], v[1], v[2]);
    fputs("    endloop\n", stlfile);
    fputs("endfacet\n", stlfile);
}




void print_mesh_for_shape (
    FILE * stlfile,
    stp_representation * rep,
    StixMtrx &rep_xform
    )
{<commit_msg>Fix facet normal handling for current released version.  The upcoming stixmesh computes the normal rather than storing it but that is not currently available in personal edition.<commit_after>/* $RCSfile: $
 * $Revision: $ $Date: $
 * Auth: <NAME> (<EMAIL>)
 * 
 * Copyright (c) 1991-2015 by STEP Tools Inc. 
 * All Rights Reserved.
 * 
 * Permission to use, copy, modify, and distribute this software and
 * its documentation is hereby granted, provided that this copyright
 * notice and license appear on all copies of the software.
 * 
 * STEP TOOLS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. STEP TOOLS
 * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A
 * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

#include <stp_schema.h>
#include <stix.h>
#include <stixmesh.h>

#include "stp2webgl.h"

// write_stl() -- write a single STL file for a STEP model.  This
// facets everything in one pass, and then work on the cached data.
// It then recursively walks down through any assemblies, applying
// transforms to the facet data and writing ASCII STL.
//

extern void facet_all_products (stp2webgl_opts * opts);
extern int write_stl (stp2webgl_opts * opts);

static void print_mesh_for_product (
    FILE * stlfile,
    stp_product_definition * pd,
    StixMtrx &starting_placement
    );

// ======================================================================

extern int write_stl (stp2webgl_opts * opts)
{    
    FILE * stlfile = stdout;
    unsigned i,sz;
    
    if (opts->do_split)
    {
	printf ("Only single STL file output currently implemented\n");
	return 2;
    }
    
    if (opts->dstfile)
    {
	stlfile = rose_fopen(opts->dstfile, "w");
	if (!stlfile) {
	    printf ("Could not open output file\n");
	    return 2;
	}
    }

    // Recursively facet all of the products in the root assemblies
    // and attach each resulting mesh to the representation item for
    // each solid.
    //
    facet_all_products(opts);

    
    fputs ("solid ", stlfile);
    if (opts-> dstfile) fputs (opts-> dstfile, stlfile);
    fputs ("\n", stlfile);

    // Now print the mesh details along with placement info
    for (i=0, sz=opts->root_prods.size(); i<sz; i++)
    {
	// The root placement is usually the identity matrix but some
	// systems put a standalone AP3D at the top to place the whole
	// thing in the global space.
	StixMtrx root_placement; 

	print_mesh_for_product (stlfile, opts->root_prods[i], root_placement);
    }

    fputs ("endsolid ", stlfile);
    if (opts-> dstfile) fputs (opts-> dstfile, stlfile);
    fputs ("\n", stlfile);

    return 0;
}



//------------------------------------------------------------
//------------------------------------------------------------
// PRINT THE FACET INFORMATION -- This follows the shape information
// attached to a single product or assembly and prints it to the STL
// file.  This is adapted from the stixmesh facet assembly sample.
//
// Since the shapes are in a tree that parallels the product tree, we
// look for attached next_assembly_usage_occurrences (NAUO) that tell
// us when we are moving into the shape of another product.
//------------------------------------------------------------
//------------------------------------------------------------



void print_triangle (
    FILE * stlfile,
    const StixMeshFacetSet * fs,
    StixMtrx &xform,
    unsigned facet_num
    )
{
    double v[3];
    double n[3];
    const StixMeshFacet * f = fs-> getFacet(facet_num);
    const char * vertexfmt = "        vertex %.15g %.15g %.15g\n";

    if (!f) return;

    // The components of the triangle verticies and vertex normals are
    // given by an index into internal tables.  Apply the transform so
    // that the facet is placed correctly in the part space.
    //
#ifdef FACET_NORMAL_NOW_COMPUTED_IN_LATEST_VERSIONS
    fs->getFacetNormal(n, f);
    stixmesh_transform_dir (n, xform, n); 
#else
    stixmesh_transform_dir (n, xform, fs-> getNormal(f-> facet_normal));
#endif
    fprintf(stlfile, "facet normal %.15g %.15g %.15g\n", n[0], n[1], n[2]);
    
    fputs("    outer loop\n", stlfile);

    stixmesh_transform (v, xform, fs-> getVertex(f-> verts[0]));
    fprintf(stlfile, vertexfmt, v[0], v[1], v[2]);

    stixmesh_transform (v, xform, fs-> getVertex(f-> verts[1]));
    fprintf(stlfile, vertexfmt, v[0], v[1], v[2]);

    stixmesh_transform (v, xform, fs-> getVertex(f-> verts[2]));
    fprintf(stlfile, vertexfmt, v[0], v[1], v[2]);
    fputs("    endloop\n", stlfile);
    fputs("endfacet\n", stlfile);
}




void print_mesh_for_shape (
    FILE * stlfile,
    stp_representation * rep,
    StixMtrx &rep_xform
    )
{
{'fraction_non_alphanumeric': 0.16763555649508508, 'fraction_numerical': 0.009724130641581228, 'mean_word_length': 2.6059451219512195, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6385415', 'n_tokens_mistral': 3097, 'n_tokens_neox': 2933, 'n_words': 1030}