/// <summary> /// Implement interface of creating a file. /// </summary> /// <returns>True if succeeded, false if failed.</returns> public override bool CreateFile() { bool succeed = true; try { FileAttributes fileAttribute = FileAttributes.Normal; if (File.Exists(m_FileName)) { fileAttribute = File.GetAttributes(m_FileName); FileAttributes tempAtt = fileAttribute & FileAttributes.ReadOnly; if (FileAttributes.ReadOnly == tempAtt) { MessageBox.Show(OpenFOAMInterfaceResource.ERR_FILE_READONLY, OpenFOAMInterfaceResource.MESSAGE_BOX_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } File.Delete(m_FileName); } stlFile = new(m_FileName); stlFile.NewLine = "\n"; fileAttribute = File.GetAttributes(m_FileName) | fileAttribute; File.SetAttributes(m_FileName, fileAttribute); //stlFile.WriteLine(/*MSG0*/"solid " + m_FileName); //file header } catch (Exception e) { OpenFOAMDialogManager.ShowDialogException(e); succeed = false; } return(succeed); }
Result IExternalApplication.OnStartup(UIControlledApplication application) { try { //set default settings Exporter.Instance.settings = new( SaveFormat.ascii, ElementsExportRange.OnlyVisibleOnes, true, false, false, false, 0, 101, 1, 100, 2 /*purgeWrite*/, 8, 7, 4 ); string appName = "OpenFOAM Interface"; RibbonPanel panel = application.CreateRibbonPanel(appName); string dirName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string assemblyname = typeof(OpenFOAMInterfaceApp).Assembly.GetName().Name; string dllName = dirName + @"\" + assemblyname + ".dll"; PushButtonData simBtnData = new("OpenFOAM Simulate", "Simulate", dllName, "OpenFOAMInterface.BIM.OpenFOAMSimulateCommand"); PushButton simulateButton = panel.AddItem(simBtnData) as PushButton; using (Stream xstr = new MemoryStream()) { Properties.Resources.openfoaminterface.Save(xstr, System.Drawing.Imaging.ImageFormat.Bmp); xstr.Seek(0, SeekOrigin.Begin); BitmapDecoder bdc = new BmpBitmapDecoder(xstr, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); simulateButton.LargeImage = bdc.Frames[0]; } simulateButton.ToolTip = "The OpenFOAM Interface for Revit is designed to produce a stereolithography file (STL) of your building model and a OpenFOAM-Config."; simulateButton.LongDescription = "The OpenFOAM Iterface for Autodesk Revit is a project designed to create an STL file from a 3D building information model for OpenFOAM with a Config-File that includes the boundary conditions for airflow simulations."; ContextualHelp help = new(ContextualHelpType.ChmFile, dirName + @"\Resources\OpenFoamInterfaceHelp.html"); simulateButton.SetContextualHelp(help); return(Result.Succeeded); } catch (Exception e) { OpenFOAMDialogManager.ShowDialogException(e); return(Result.Failed); } }
/// <summary> /// Implement interface of creating a file. /// </summary> /// <returns>True if succeeded, false if failed.</returns> public override bool CreateFile() { bool succeed = true; try { FileAttributes fileAttribute = FileAttributes.Normal; if (File.Exists(m_FileName)) { fileAttribute = File.GetAttributes(m_FileName); FileAttributes tempAtt = fileAttribute & FileAttributes.ReadOnly; if (FileAttributes.ReadOnly == tempAtt) { MessageBox.Show(OpenFOAMInterfaceResource.ERR_FILE_READONLY, OpenFOAMInterfaceResource.MESSAGE_BOX_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(false); } File.Delete(m_FileName); } fileWriteStream = new FileStream(m_FileName, FileMode.Create); fileAttribute = File.GetAttributes(m_FileName) | fileAttribute; File.SetAttributes(m_FileName, fileAttribute); binaryWriter = new BinaryWriter(fileWriteStream); binaryWriter.BaseStream.Seek(0, SeekOrigin.Begin); // write 80 bytes to STL file as the STL file entity name // and preserve 4 bytes space for Triangular Number Section byte[] entityName = new byte[84]; entityName[0] = (byte)/*MSG0*/ 'n'; entityName[1] = (byte)/*MSG0*/ 'a'; entityName[2] = (byte)/*MSG0*/ 'm'; entityName[3] = (byte)/*MSG0*/ 'e'; for (int i = 4; i < 84; i++) { entityName[i] = (byte)/*MSG0*/ '\0'; } binaryWriter.Write(entityName); } catch (Exception e) { OpenFOAMDialogManager.ShowDialogException(e); succeed = false; } return(succeed); }
/// <summary> /// Implement interface of adding triangular number section. /// </summary> /// <returns>True if succeeded, false if failed.</returns> public override bool AddTriangularNumberSection() { bool succeed = true; try { binaryWriter.BaseStream.Seek(80, SeekOrigin.Begin); //write the tringle number to the STL file using 4 bytes. binaryWriter.Write(m_TriangularNumber); } catch (Exception e) { OpenFOAMDialogManager.ShowDialogException(e); succeed = false; } return(succeed); }
/// <summary> /// Implement interface of writing one section include normal and vertex. /// </summary> /// <param name="normal">Facet normal.</param> /// <param name="vertexArr">Vertex array.</param> /// <returns>True if succeeded, false if failed.</returns> public override bool WriteSection(Autodesk.Revit.DB.XYZ normal, double[] vertexArr) { bool succeed = true; try { StringBuilder normalSb = new(/*MSG0*/ " facet normal "); for (int j = 0; j < 3; j++) { //Numberformat should be english in stl string en_double_normal = normal[j].ToString(System.Globalization.CultureInfo.GetCultureInfo("en-US").NumberFormat); normalSb.Append(en_double_normal).Append(/*MSG0*/ " "); //normalSb.Append(normal[j]).Append(/*MSG0*/" "); } stlFile.WriteLine(normalSb); stlFile.WriteLine(/*MSG0*/ " outer loop"); for (int i = 0; i < 3; i++) { StringBuilder vertexSb = new(/*MSG0*/ " vertex "); for (int j = 0; j < 3; j++) { //Numberformat should be english in stl string en_double_vertex = vertexArr[i * 3 + j].ToString(System.Globalization.CultureInfo.GetCultureInfo("en-US").NumberFormat); vertexSb.Append(en_double_vertex).Append(/*MSG0*/ " "); //vertexSb.Append(vertexArr[i * 3 + j]).Append(/*MSG0*/" "); } stlFile.WriteLine(vertexSb); } stlFile.WriteLine(/*MSG0*/ " endloop"); stlFile.WriteLine(/*MSG0*/ " endfacet"); } catch (Exception e) { OpenFOAMDialogManager.ShowDialogException(e); succeed = false; } return(succeed); }
/// <summary> /// Implement interface of writing one section include normal and vertex. /// </summary> /// <param name="normal">Facet normal.</param> /// <param name="vertexArr">Vertex array.</param> /// <returns>True if succeeded, false if failed.</returns> public override bool WriteSection(Autodesk.Revit.DB.XYZ normal, double[] vertexArr) { bool succeed = true; try { // write 3 float numbers to stl file using 12 bytes. for (int j = 0; j < 3; j++) { binaryWriter.Write((float)normal[j]); } for (int i = 0; i < 9; i++) { binaryWriter.Write((float)vertexArr[i]); } // add color to stl file using two bytes. if (m_color != null) { binaryWriter.Write((ushort)(((m_color.Red) >> 3) | (((m_color.Green) >> 3) << 5) | (((m_color.Blue) >> 3) << 10))); } else { // add two spaces to stl file using two bytes. byte[] anotherSpace = new byte[2]; anotherSpace[0] = (byte)/*MSG0*/ '\0'; anotherSpace[1] = (byte)/*MSG0*/ '\0'; binaryWriter.Write(anotherSpace); } } catch (Exception e) { OpenFOAMDialogManager.ShowDialogException(e); succeed = false; } return(succeed); }