Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Exported .stl file works on developer pc but not the other users

0 REPLIES 0
Reply
Message 1 of 1
mahdi_jafari8CJYM
3730 Views, 0 Replies

Exported .stl file works on developer pc but not the other users

Hi @jeremytammik and all

 

I have recently made a plugin for Revit 2023 which can modify building elements. I had to create another button for exporting to STL. As the developer, I'm exporting my Revit document to STL using my own "export to STL" function. The exported STL file works with Prusa and other software, but when I share my plugin files (.dll & .addin) with other users, they get an error when opening the STL file in Prusa. (should i gave them more files?)



can someone please help me with this issue?


here is the code:

using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using System;
using System.IO;
using System.Windows;
using Microsoft.Win32;
using System.Collections.Generic;
using System.Linq;

namespace [XXXXXXXXXXXX]
{
[Transaction(TransactionMode.Manual)]
public class ExportToSTL : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
Document document = uiDoc.Document;

// Show options form to user
ExportOptionsControl optionsControl = new ExportOptionsControl();
Window window = new Window
{
Title = "STL Export Options",
Content = optionsControl,
SizeToContent = SizeToContent.WidthAndHeight,
WindowStartupLocation = WindowStartupLocation.CenterScreen
};

if (window.ShowDialog() != true)
{
return Result.Cancelled;
}

// Retrieve user options
var format = optionsControl.ExportFormat;
var resolution = optionsControl.Resolution;
var units = optionsControl.Units;

// Define the output path as savefile dialog
string outputPath = string.Empty;

SaveFileDialog saveFileDialog = new SaveFileDialog
{
Filter = "STL files (*.stl)|*.stl"
};
if (saveFileDialog.ShowDialog() == true)
{
outputPath = saveFileDialog.FileName;
}
else
{
return Result.Cancelled;
}

// Implement STL Export logic here
try
{
ExportSTL(document, uiDoc, outputPath, format, resolution, units);

TaskDialog.Show("Export STL", "Model exported successfully to " + outputPath);
return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}

void ExportSTL(Document doc, UIDocument uiDoc, string exportPath, string format, string resolution, string units)
{
// Determine the scaling factor based on the selected units
double scaleFactor = 1.0;
switch (units)
{
case "Millimeters":
scaleFactor = 304.8; // 1 foot = 304.8 millimeters
break;
case "Meters and centimeters":
scaleFactor = 0.3048; // 1 foot = 0.3048 meters
break;
case "Inches":
scaleFactor = 12.0; // 1 foot = 12 inches
break;
}

using (StreamWriter writer = new StreamWriter(exportPath))
{
writer.WriteLine("solid model");

// Collect selected elements or visible elements in the current view
ICollection<ElementId> selectedIds = uiDoc.Selection.GetElementIds();
FilteredElementCollector collector = new FilteredElementCollector(doc);

if (selectedIds.Count > 0)
{
collector = new FilteredElementCollector(doc, selectedIds).WhereElementIsNotElementType();
}
else
{
collector = new FilteredElementCollector(doc, uiDoc.ActiveView.Id)
.WhereElementIsNotElementType()
.WhereElementIsViewIndependent();
}

foreach (Element element in collector)
{
if (element is FamilyInstance || element is Wall || element is Floor || element is RoofBase || element is HostObject)
{
Options geomOptions = new Options();
GeometryElement geomElement = element.get_Geometry(geomOptions);

if (geomElement != null)
{
foreach (GeometryObject geomObj in geomElement)
{
if (geomObj is Solid solid)
{
ExportSolidToSTL(solid, writer, scaleFactor);
}
else if (geomObj is GeometryInstance geomInstance)
{
GeometryElement instanceGeometry = geomInstance.GetInstanceGeometry();
foreach (GeometryObject instanceGeomObj in instanceGeometry)
{
if (instanceGeomObj is Solid instanceSolid)
{
ExportSolidToSTL(instanceSolid, writer, scaleFactor);
}
}
}
}
}
}
}

writer.WriteLine("endsolid model");
}
}

void ExportSolidToSTL(Solid solid, StreamWriter writer, double scaleFactor)
{
foreach (Face face in solid.Faces)
{
Mesh mesh = face.Triangulate();

for (int i = 0; i < mesh.NumTriangles; i++)
{
MeshTriangle tri = mesh.get_Triangle(i);

XYZ normal = (tri.get_Vertex(1) - tri.get_Vertex(0)).CrossProduct(tri.get_Vertex(2) - tri.get_Vertex(0)).Normalize();
writer.WriteLine("facet normal {0} {1} {2}", normal.X, normal.Y, normal.Z);
writer.WriteLine(" outer loop");

for (int j = 0; j < 3; j++)
{
XYZ vertex = tri.get_Vertex(j) * scaleFactor;
writer.WriteLine(" vertex {0} {1} {2}", vertex.X, vertex.Y, vertex.Z);
}

writer.WriteLine(" endloop");
writer.WriteLine("endfacet");
}
}
}
}
}

Tags (1)
Labels (4)
0 REPLIES 0

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Rail Community


Autodesk Design & Make Report