Skip to content

Commit

Permalink
Add export_sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
simonp0420 committed Apr 19, 2023
1 parent 2285a19 commit 67d7e4c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
5 changes: 0 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,8 @@ Unitful = "1.7"
julia = "1"

[extras]
Cubature = "667455a9-e2ce-5579-9412-b964f529a492"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
HCubature = "19dc6840-f33b-545b-b366-655c7e3ffd49"
Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
docs = ["Documenter", "Literate"]
test = ["Test", "Cubature", "HCubature", "SafeTestsets"]
3 changes: 2 additions & 1 deletion src/PSSFSS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ include("Outputs.jl")
include("FastSweep.jl")

using .Rings
@reexport using .Sheets: Sheet, RWGSheet, read_sheet_data, nodecount, facecount, edgecount
@reexport using .Sheets: Sheet, RWGSheet, read_sheet_data, nodecount, facecount, edgecount,
export_sheet, STL_ASCII, STL_BINARY
using .RWG: setup_rwg, rwgbfft!, RWGData
using .GSMs: GSM, cascade, cascade!, gsm_electric_gblock, gsm_magnetic_gblock,
gsm_slab_interface, translate_gsm!, choose_gblocks, Gblock, pecgsm, pmcgsm
Expand Down
53 changes: 53 additions & 0 deletions src/Sheets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ module Sheets

export RWGSheet, read_sheet_data, write_sheet_data, find_unique_periods
export rotate!, translate!, combine, recttri, SV2, MV2, nodecount, facecount, edgecount
export export_sheet, STL_ASCII, STL_BINARY

using StaticArrays: SVector, MVector, SMatrix
using ..PSSFSSLen
using JLD2
using LinearAlgebra: norm
using RecipesBase
using Printf: @printf


const MV2 = MVector{2,Float64}
Expand Down Expand Up @@ -152,6 +154,57 @@ function write_sheet_data(filename::AbstractString, sheet::RWGSheet)
end
end

abstract type CAD_Export end
struct STL_ASCII <: CAD_Export end
struct STL_BINARY <: CAD_Export end

export_sheet(fname::AbstractString, sheet::RWGSheet, t) = error("Unknown CAD export type $t")

"""
export_sheet(fname::AbstractString, sheet::RWGSheet, export_type)
Export an `RWGSheet` triangulation to an STL CAD file. `export_type` may be either
`STL_ASCII` or `STL_BINARY`.
"""
function export_sheet(fname::AbstractString, sheet::RWGSheet, export_type::Type{STL_ASCII})
open(fname, "w") do io
write(io, "solid vcg\n") # header
for i in eachcol(sheet.fv)
@printf(io, " facet normal %e %e %e\n", 0.0, 0.0, 1.0)
write(io, " outer loop\n")
for v in @view sheet.ρ[i]
@printf(io, " vertex %e %e %e\n", v[1], v[2], 0.0)
end
write(io, " endloop\n")
write(io, " endfacet\n")
end
write(io,"endsolid vcg\n")
end
return nothing
end

function export_sheet(fname::AbstractString, sheet::RWGSheet, export_type::Type{STL_BINARY})
open(fname, "w") do io
# Implementation made according to https://en.wikipedia.org/wiki/STL_%28file_format%29#Binary_STL
for i in 1:80 # write empty header
write(io, 0x00)
end

write(io, UInt32(facecount(sheet))) # write triangle count
n = (0.0f0, 0.0f0, 1.0f0)
for i in eachcol(sheet.fv)
foreach(j -> write(io, n[j]), 1:3)
for point in @view sheet.ρ[i]
point3 = (Float32(point[1]), Float32(point[2]), 0.0f0)
foreach(p -> write(io, p), point3)
end
write(io, 0x0000) # write 16bit empty bit
end
end
return nothing
end



"""
find_unique_periods(junction::Vector{Int}, sheets)
Expand Down
15 changes: 15 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[deps]
Cubature = "667455a9-e2ce-5579-9412-b964f529a492"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
HCubature = "19dc6840-f33b-545b-b366-655c7e3ffd49"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
MeshIO = "7269a6da-0436-5bbc-96c2-40638cbb6118"
Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa"
MetalSurfaceImpedance = "d1286fa9-018f-4163-8b95-1473a4173f0d"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
24 changes: 24 additions & 0 deletions test/RWGSheet_test.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using PSSFSS
import PSSFSS.Sheets: SV2, recttri, combine, read_sheet_data, write_sheet_data
using Test
using FileIO: load

bl = SV2([0.0, 0.0])
tr = SV2([1.0, 1.0])
Expand Down Expand Up @@ -85,4 +86,27 @@ end
write_sheet_data(fname, sh3)
sh4 = read_sheet_data(fname)
@test sh3 == sh4
end

@testset "export_sheet" begin
s1 = [1, 0]; s2 = [0, 1]
b = [0.12, 0.2, 0.3]
sides = 50; ntri = 2800; units = cm
sheet = sinuous(; arms=4, b, w=0.03, rc=0.05, s1, s2,
L2=0.95, w2=0.03, c2=0.12, g=0.04, sides, ntri, units)
td = tempdir()
asciifile = "temp_ascii.stl"
binaryfile= "temp_binary.stl"
export_sheet(joinpath(td, asciifile), sheet, STL_ASCII)
export_sheet(joinpath(td, binaryfile), sheet, STL_BINARY)
msha = load(joinpath(td, asciifile))
mshb = load(joinpath(td, binaryfile))

@test facecount(sheet) == length(msha) == length(mshb)
positions = [[Float32(v[1]), Float32(v[2]), 0.0f0] for v in vec(sheet.ρ[sheet.fv])]
@test mshb.position == positions
@test msha.position mshb.position
@test all(==([0,0,1]), msha.normals)
@test all(==([0,0,1]), mshb.normals)

end

0 comments on commit 67d7e4c

Please sign in to comment.