cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

WriteSTL Custom Function

No ratings

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.

Comments

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

Cheers.

Just attached

Below are screenshots of the contents of this file:

Pic_1.PNG

Pic_2.PNG

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

Thank you Vladimir for sharing this work.

You're welcome.

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

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.

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

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

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

I'll add a function like tis and see. Hopefully the surfaces will be all face up. Stay tuned.

Begin Function CalculateSurfaceNormal (Input Triangle) Returns Vector

        Set Vector U to (Triangle.p2 minus Triangle.p1)

        Set Vector V to (Triangle.p3 minus Triangle.p1)

        Set Normal.x to (multiply U.y by V.z) minus (multiply U.z by V.y)

        Set Normal.y to (multiply U.z by V.x) minus (multiply U.x by V.z)

        Set Normal.z to (multiply U.x by V.y) minus (multiply U.y by V.x)

        Returning Normal

End Function

So I have a gen_norms function. Can easily be added to Mathcad 14-15 version. In Prime I have have updated the csutomer function to take a second matrix.

Now my Creo license is dead!! No way to test I subtracted the sides and cross producted the vectors correctly.

I'll keep plugging.

Up and running and will be posting a new WriteSTL.zip today (2/24/2014) with worksheets and source code for Prime. After researching I found that OpenGL renderers call glNormal3f() when normals are not present. This is why my renderings in Creo do not look any different.Mystery solved.

But as you have mentioned the nromals being present can be useful for CFD and perhaps others, they they are now included in the STL outputfile.

Thanks for the tip.

Dear Sir,

 

Is .dll file available for windows 10 - 64 bit.??

I am working with Mathcad 6.0

 

If available please share.

It will helpful for me.

You can find it in attachment.

Dear sir, These files are compatible for Windows 7. I need .dll which works for windows 10 & Mathcad 6.0. If available please provide.
Version history
Last update:
‎Apr 29, 2013 01:41 PM
Updated by:
Labels (1)