Skip to main content
5-Regular Member
April 29, 2013

WriteSTL Custom Function

  • April 29, 2013
  • 15 replies
  • 11382 views

Full list of worksheets

 

WriteSTL allows Mathcad users to create 3D models mathematically using PTC Mathcad then save them as STL files, perfectly suited for incorporation to CAD assemblies.

 

Along with the README, the WriteSTL.zip file contains two worksheets, showing 2 different matrix styles possible to generate 3D models. WriteSTL takes an array of triangle vertices. The included PTC Mathcad worksheets demonstrate inline routines to convert each matrix type to triangle vertices arrays.

 

Also included is the WriteSTL Custom Function compiled and linked for the Windows 7 64-bit platform. The MS Visual Studio project with source is included for anyone needing to build for different Windows Platforms. Microsoft has a free, Express version of Microsoft Visual Studio customers are using to successfully build PTC Mathcad Custom Functions.

15 replies

1-Visitor
May 19, 2013

Can we please see the file in older version for Mathcad 14.

Cheers.

5-Regular Member
May 20, 2013

Just attached

24-Ruby III
November 2, 2013

Below are screenshots of the contents of this file:

Pic_1.PNG

Pic_2.PNG

24-Ruby III
November 15, 2013

Generate STL (STereoLithography) file in Mathcad 14/15: http://communities.ptc.com/docs/DOC-4620

1-Visitor
November 16, 2013

Thank you Vladimir for sharing this work.

24-Ruby III
November 16, 2013

You're welcome.

1-Visitor
January 3, 2014

Hi Anna Giangregorio, Brent Edmonds, MathCad

Thank you for this upload.  Especially the source code so it can be now also be made for windows 7, 32 bit.

Very usefull and much appreciated.  STL is standard format read (or can be converted to CFD meshes) by many CFD open source fluid flow solvers.

Regards

Terry

1-Visitor
January 4, 2014

Hi,

Function can be improved by adding the normals to the triangles making up the STL surface.  It only uses 0,0,0. 

Surface normals are used by software that uses STL files to determine which is the front and back orientation.  They can also be used with a trick to help calculate the area of a triangulated STL surface.

Here is adjusted cpp file.  Input is a matrix with now four instead of three rows per triangle.  First row is normal of face, followed by original three rows of coordinates of corners. 

MathCad can calculate these normals and adjust the matrix before submission to the writeSTL function.

>>>>>>>>>>>>>

1-Visitor
January 4, 2014

Terry Hendicott 03/01/2014 4:21 PM

Hi,

Function can be improved by adding the normals to the triangles making up the STL surface.  It only uses 0,0,0. 

Surface normals are used by software that uses STL files to determine which is the front and back orientation.  They can also be used with a trick to help calculate the area of a triangulated STL surface.

Here is adjusted cpp file.  Input is a matrix with now four instead of three rows per triangle.  First row is normal of face, followed by original three rows of coordinates of corners. 

MathCad can calculate these normals and adjust the matrix before submission to the writeSTL function.

MathCad_Triangle+Normals.jpg

Mathcad_Triangle+Normals+2.jpg

>>>>>>>>>>>>>

// WriteSTL.cpp : Defines the exported functions for the DLL application.
//

#include "StdAfx.h"roes
#include "mcadincl.h"
#include <stdio.h>
   
    LRESULT WriteSTLFunction( LPCOMPLEXSCALAR result,
        LPMCSTRING      filename,
                                LPCOMPLEXARRAY     mat );

    FUNCTIONINFO    WriteSTL =
    {
    "WriteSTL",                                    // Name by which mathcad will recognize the function
    "filename,mat",                                            // transpose will be called as WriteSTL(filename,mat)
    "returns a success"  ,                   // everything work?
    (LPCFUNCTION)WriteSTLFunction,                 // pointer to the executible code
    COMPLEX_SCALAR,                                  // the output is a result flag
    2,                                              // the function takes on 2 arguments
    { STRING, COMPLEX_ARRAY}    // the input type is a complex array
    };
   
   
    LRESULT WriteSTLFunction( LPCOMPLEXSCALAR result,
        LPMCSTRING filename,
                       LPCOMPLEXARRAY     mat   )
    {
        FILE *fp;
  int i;
  fp = fopen(filename->str,"w");
  // Mathcad matrices are col x row via API, not row x col like the rest of the world
  fprintf(fp,"%s\n","solid Mathcad_Surface");
  for (i=0; i<mat->rows; i+=4) {
   fprintf(fp,"facet normal %f %f %f\n",mat->hReal[0][i],mat->hReal[1][i],mat->hReal[2][i]));
   fprintf(fp,"outer loop\n");
   fprintf(fp,"vertex %f %f %f\n",mat->hReal[0][i+1],mat->hReal[1][i+1],mat->hReal[2][i+1]);
   fprintf(fp,"vertex %f %f %f\n",mat->hReal[0][i+2],mat->hReal[1][i+2],mat->hReal[2][i+2]);
   fprintf(fp,"vertex %f %f %f\n",mat->hReal[0][i+3],mat->hReal[1][i+3],mat->hReal[2][i+3]);
   fprintf(fp,"endloop\n");
   fprintf(fp,"endfacet\n");
  }
  fprintf(fp,"%s\n","endsolid Mathcad_Surface");
  fclose(fp);
        result->real = 1;
        return 0;               // return 0 to indicate there was no error
           
    }  

>>>>>>>>>>>>

Can provide the MathCad14 file with the small program that adds the normals but no idea how to upload a MathCad file only images of limited size and videos can be added in this web page.

Regards

1-Visitor
February 21, 2014

Hi Terry,

this is great. I do not see your attached CPP file. can you email me? Then I can get it posted.

And I can share with VladimirN and maybe he'll update his Mathcad 14 version.

John