BeginPackage["Acro`"]

primativeplot3d::usage="primativeplot3d[{fx, fy, fz}, {t, tmin, tmax}]
produces a list of points of a space curve parametrized by a variable
t which runs from tmin to tmax.
primativeplot3d[{fx, fy, fz}, {t, tmin, tmax}, {u, umin, umax}]
produces a list of points by polygons of a surface parametrized by
t and u."

WriteAcroPoints::usage="WriteAcroPoints[{color},file,{fx, fy, fz},
{t, tmin, tmax}] outputs to a file in acrospin point format the plot of
a space curve.
WriteAcroPoints[{color},file,{fx, fy, fz}, {t, tmin, tmax}, {u, umin, umax}]
outputs to a file in acrospin point format the plot of a surface."

WriteAcroLines::usage="WriteAcroLines[{color1,color2},file,{fx, fy, fz},
{t, tmin, tmax}] outputs to a file in acrospin line format the plot of
a space curve.
WriteAcroLines[{color1,color2},{fx, fy, fz}, {t, tmin, tmax}, {u, umin, umax}]
outputs to a file in acrospin line format the plot of a surface."

Begin["`Private`"]
primativeplot3d[exp_,{t_,tmin_,tmax_},opts___]:=
First[ParametricPlot3D[exp,
{t,tmin,tmax},opts,
DisplayFunction->Identity]] /.
(Line[pts_] :> pts)

primativeplot3d[exp_,{u_,umin_,umax_},{v_,vmin_,vmax_},opts___]:=
First[ParametricPlot3D[exp,
{u,umin,umax},{v,vmin,vmax},opts,
DisplayFunction->Identity]] /.
(Polygon[pts_] :> pts)

WriteAcroPoints[{color_},file_String,curve_,
{t_,tmin_,tmax_},opts___]:=
Module[{tmp1,tmp2,tmp3,tmp4},
OpenWrite[file];
WriteString[file,"Set Color "];
WriteString[file,color];
WriteString[file,"\n"];
WriteString[file,"PointList X Y Z\n"];
tmp1=primativeplot3d[curve//Evaluate,
{t,tmin,tmax},opts];
tmp2=Union[Flatten[tmp1,1]];
tmp3=Chop[tmp2,10^-3];
tmp4=Map[PaddedForm[#,{4,4}]&,tmp3,{2}];
Scan[
(WriteString[file,First[#]];
Scan[WriteString[file," ",#]&,Rest[#]];
WriteString[file,"\n"])&,
tmp4];
Close[file]]

WriteAcroPoints[{color_},file_String,surface_,
{u_,umin_,umax_},{v_,vmin_,vmax_},opts___]:=
Module[{tmp1,tmp2,tmp3,tmp4},
OpenWrite[file];
WriteString[file,"Set Color "];
WriteString[file,color];
WriteString[file,"\n"];
WriteString[file,"PointList X Y Z\n"];
tmp1=primativeplot3d[surface//Evaluate,
{u,umin,umax},{v,vmin,vmax},opts];
tmp2=Union[Flatten[tmp1,1]];
tmp3=Chop[tmp2,10^-3];
tmp4=Map[PaddedForm[#,{4,4}]&,tmp3,{2}];
Scan[
(WriteString[file,First[#]];
Scan[WriteString[file," ",#]&,Rest[#]];
WriteString[file,"\n"])&,
tmp4];
Close[file]]

WriteAcroLines[{color_},file_String,curve_,
{t_,tmin_,tmax_},opts___]:=
Module[{tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7,tmp8},
OpenWrite[file];
WriteString[file,"Set Color "];
WriteString[file,color];
WriteString[file,"\n"];
WriteString[file,"EndPointList name X Y Z\n"];
tmp1=primativeplot3d[curve//Evaluate,
{t,tmin,tmax},opts];
tmp2=Flatten[tmp1,1];
tmp3=Chop[tmp2,10^-3];
tmp4=Map[PaddedForm[#,{4,4}]&,tmp3,{2}];
tmp5=Table[
{StringForm["VERTEX``",j],tmp4[[j]]},
{j,Length[tmp3]}];
tmp6=Map[Flatten[#,1]&,tmp5,{1}];
Scan[
(WriteString[file,First[#]];
Scan[WriteString[file," ",#]&,Rest[#]];
WriteString[file,"\n"])&,
tmp6];
WriteString[file,"\nLineList from to\n"];
tmp7=Table[
StringForm["VERTEX`` VERTEX``\n",j,j+1],
{j,Length[tmp3]-1}];
Scan[WriteString[file,#,"\n"]&,tmp7];
Close[file]]

WriteAcroLines[{color1_:12,color2_:14},
file_String,surface_,
{u_,umin_,umax_},{v_,vmin_,vmax_},opts___]:=
Module[{tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,
tmp7,tmp8,tmp9,tmp10,tmp11,tmp12},
OpenWrite[file];
WriteString[file,"EndPointList name X Y Z\n\n"];
tmp1=primativeplot3d[surface//Evaluate,
{u,umin,umax},{v,vmin,vmax},opts];
tmp2=Chop[tmp1,10^-3];
tmp3=Map[PaddedForm[#,{4,4}]&,tmp2,{3}];
tmp4=Table[
{StringForm["POLY``VERTEX``",j,k],tmp3[[j,k]]},
{j,Length[tmp3]},{k,4}];
tmp5=Map[Flatten[#,1]&,tmp4,{2}];
tmp6=Flatten[tmp5,1];
Scan[WriteString[file," ",#]&,Rest[#]];
Scan[
(WriteString[file,First[#]];
Scan[WriteString[file," ",#]&,Rest[#]];
WriteString[file,"\n"])&,
tmp6];
WriteString[file,"\nLineList from to Color Layer\n\n"];
tmp7=Table[
StringForm["POLY``VERTEX`` POLY``VERTEX`` `` ``\n",
j,1,j,2,color1,1],{j,Length[tmp3]}];
tmp8=Table[
StringForm["POLY``VERTEX`` POLY``VERTEX`` `` ``\n",
j,4,j,3,color1,1],{j,Length[tmp3]}];
tmp9=Join[tmp7,tmp8];
Scan[WriteString[file,#,"\n"]&,tmp9];
tmp10=Table[
StringForm["POLY``VERTEX`` POLY``VERTEX`` `` ``\n",
j,1,j,4,color2,2],{j,Length[tmp3]}];
tmp11=Table[
StringForm["POLY``VERTEX`` POLY``VERTEX`` `` ``\n",
j,3,j,2,color2,2],{j,Length[tmp3]}];
tmp12=Join[tmp10,tmp11];
Scan[WriteString[file,#,"\n"]&,tmp12];
Close[file]]

End[]

EndPackage[]