
SetFactory("OpenCASCADE");

//--------------------------------

ri=0.1115;
re=0.1575;
rcat=0.0248;
rgrafite=0.095;
rceramic=0.175;
rsphere = 0.10;
ds=0.007;
rbox=0.20;
dbox=0.25;
inter=0.5;
larm=1;
Rarm=0.04;
Teta=45;   // Rotation around Y
Psi = 0;   // Rotation around z
Beta=30;   // Panel rotation
TetaFF=30; // Far Field rotation
Hsat=3.0;
Wsat=1.4;
Dsat=1.0;
Zthrust=-0.5;

Lpanel=4;
Hpanel=2;
Xpanel=3;
Zpanel=Hsat/2.0-0.50;

Rff=3.1;
Lff = Xpanel+Lpanel-Rff/2.0;
Zff=Zthrust;

Teta = Teta*Pi/180.0;
Psi = Psi*Pi/180.0;
Beta = Beta*Pi/180.0;
TetaFF = TetaFF*Pi/180.0;

//--------------------------------

Merge "spline.geo";

nl=newl;

Circle(nl  ) = {-0, -inter/2, 0, rcat, 0, 2*Pi};
Circle(nl+1) = {-0, -inter/2, 0, rgrafite, 0, 2*Pi};
Circle(nl+2) = {-0, -inter/2, 0, ri-ds, 0, 2*Pi};
Circle(nl+3) = {-0, -inter/2, 0, ri, 0, 2*Pi};
Circle(nl+4) = {-0, -inter/2, 0, re, 0, 2*Pi};
Circle(nl+5) = {-0, -inter/2, 0, re+ds, 0, 2*Pi};
Circle(nl+6) = {-0, -inter/2, 0, rceramic, 0, 2*Pi};

Rotate {{0, 1, 0}, {0, 0, 0}, Pi/2} {Curve{nl:nl+6};}

Translate {-ds, 0, 0} {
  Curve{nl+3}; Curve{nl+4}; 
}

ll=newll;
ns = news;

Curve Loop(ll) = {nl};
Curve Loop(ll+1) = {nl+1};
Curve Loop(ll+2) = {nl+2};
Curve Loop(ll+3) = {nl+3};
Curve Loop(ll+4) = {nl+4};
Curve Loop(ll+5) = {nl+5};
Curve Loop(ll+6) = {nl+6};

Plane Surface(ns) = {ll};
Plane Surface(ns+1) = {ll+1, ll};
Plane Surface(ns+2) = {ll+2, ll+1};
Plane Surface(ns+3) = {ll+4, ll+3};
Plane Surface(ns+4) = {ll+6, ll+5};

a() = Ruled ThruSections{ll+3,ll+2};
a() = Ruled ThruSections{ll+5,ll+4};

nl=newl;

Circle(nl  ) = {-0, inter/2, 0, rcat, 0, 2*Pi};
Circle(nl+1) = {-0, inter/2, 0, rgrafite, 0, 2*Pi};
Circle(nl+2) = {-0, inter/2, 0, ri-ds, 0, 2*Pi};
Circle(nl+3) = {-0, inter/2, 0, ri, 0, 2*Pi};
Circle(nl+4) = {-0, inter/2, 0, re, 0, 2*Pi};
Circle(nl+5) = {-0, inter/2, 0, re+ds, 0, 2*Pi};
Circle(nl+6) = {-0, inter/2, 0, rceramic, 0, 2*Pi};

Rotate {{0, 1, 0}, {0, 0, 0}, Pi/2} {Curve{nl:nl+6};}

Translate {-ds, 0, 0} {
  Curve{nl+3}; Curve{nl+4}; 
}

ll=newll;
ns = news;

Curve Loop(ll) = {nl};
Curve Loop(ll+1) = {nl+1};
Curve Loop(ll+2) = {nl+2};
Curve Loop(ll+3) = {nl+3};
Curve Loop(ll+4) = {nl+4};
Curve Loop(ll+5) = {nl+5};
Curve Loop(ll+6) = {nl+6};

Plane Surface(ns) = {ll};
Plane Surface(ns+1) = {ll+1, ll};
Plane Surface(ns+2) = {ll+2, ll+1};
Plane Surface(ns+3) = {ll+4, ll+3};
Plane Surface(ns+4) = {ll+6, ll+5};

a() = Ruled ThruSections{ll+3,ll+2};
a() = Ruled ThruSections{ll+5,ll+4};

Extrude {-dbox, 0, 0} {Curve{1}; Curve{2};}

ll = newll;

Curve Loop(ll+1) = {1, 2};
Curve Loop(ll+2) = {9};
Curve Loop(ll+3) = {25};
Plane Surface(35) = {ll+1, ll+2, ll+3};

Cylinder(1) = {-dbox-0.10, 0, 0, 0.1, 0, 0, rsphere, 2*Pi};

Delete {
  Volume{1}; 
}

Delete {
  Surface{37}; 
}

Curve Loop(44) = {30, 31};
Curve Loop(45) = {32};
Plane Surface(39) = {44, 45};
Surface Loop(2) = {39, 33, 34, 35, 14, 16, 13, 15, 12, 11, 10, 30, 32, 29, 31, 28, 27, 26, 36, 38};

Volume(1) = {2};

//////////////////////////////////////////

Delete {
  Point{2:74}; 
}

//// ROTATIONS ///////////////////////////////////////

Point(9999)={-ds, inter/2.0,   0.00000, 1.0};

/// ROTATE AROUND Y

Rotate {{0, 1, 0}, {-dbox-0.1, 0, 0}, Teta} {Volume{1};Point{9999};}

/// ROTATE AROUND Z

Rotate {{0, 0, 1}, {-dbox-0.1, 0, 0}, Psi} {Volume{1};Point{9999};}

//////////////////////////////////////////////////////


// Spacecraft Box -------------------------------------

Box(2) = {-Wsat/2, -Dsat/2, -Hsat/2, Wsat, Dsat, Hsat};

//-----------------------------------------------------

// Thrusters in place ---------------------------------

Translate {larm+Wsat/2+0.1+dbox, 0, Zthrust} {Volume{1};Point{9999}; }

//// ARM -----------------------------------------------------

Cylinder(3) = {Wsat/2, 0, Zthrust, larm+0.01, 0, 0, Rarm, 2*Pi};
Sphere(4) = {larm+Wsat/2, 0, Zthrust, rsphere, -Pi/2, Pi/2, 2*Pi};

BooleanUnion{ Volume{3}; Delete; }{ Volume{4}; Delete; }
BooleanFragments{ Volume{3}; Delete; }{ Volume{1}; }

Recursive Delete {Volume{4}; }

BooleanFragments{ Volume{3}; Delete; }{ Volume{1}; Delete; }

Delete {Volume{3}; Volume{1}; }

Delete {Surface{46}; }

BooleanFragments{ Surface{22}; Delete; }{ Surface{49}; Delete; }

Delete {Volume{2}; }

Delete {Surface{22};Surface{49};}

Surface Loop(8) = {23, 25, 21, 26, 24, 50, 47, 48, 45, 27, 28, 
                   29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
                   40, 41, 42, 43, 44};

Volume(1) = {8};

//// PANEL ----------------------------------------------------

nv = newv;

Box(nv) = {Xpanel, -0.01, Zpanel-Hpanel/2.0, Lpanel, 0.02, Hpanel};

/// ROTATE PANEL

Rotate {{1, 0, 0}, {Xpanel, 0.0, Zpanel}, Beta} {Volume{nv}; }

/// FAR FIELD

nv=newv;

Cylinder(nv+1) = {-Wsat/2+Rff/2, 0, Zff, Lff+Wsat/2-Rff/2, 0, 0, Rff, 2*Pi};
Sphere(nv+2) = {Lff, 0, Zff, Rff, -Pi/2, Pi/2, 2*Pi};
Sphere(nv+3) = {-Wsat/2+Rff/2, 0, Zff, Rff, -Pi/2, Pi/2, 2*Pi};

BooleanUnion{ Volume{nv+1}; Delete; }{ Volume{nv+2}; Delete; }
BooleanUnion{ Volume{nv+4}; Delete; }{ Volume{nv+3}; Delete; }

/// BACKGROUND ------------

Merge "background.geo";
//Merge "background_b.geo";

/// VOLUME ----------------------------------------------------------

BooleanDifference{ Volume{109}; Delete; }{ Volume{1}; Volume{108}; Delete; }

Transfinite Curve {41, 40} = 13 Using Progression 1;


Physical Surface("Far Field", 1000) = {58, 57, 60, 59};
Physical Surface("Spacecraft", 1001) = {26, 21, 23, 24, 25, 50};
Physical Surface("TA", 1002) = {47, 48, 45, 27, 28, 29, 30};
Physical Surface("Ceramics", 1003) = {31, 32, 34, 35, 38, 39, 41, 42};
Physical Surface("Internal Pole", 1004) = {43, 36};
Physical Surface("Cathode", 1005) = {37, 44};
Physical Surface("Panel upper", 1006) = {53};
Physical Surface("Panel lower", 1007) = {54, 56, 52, 55, 51};
Physical Surface("Thruster #1", 1009) = {33};
Physical Surface("Thruster #2", 1010) = {40};

Physical Volume("Volume", 1008) = {110};

s() = Surface In BoundingBox{0.6,-1,Zthrust-1,2,1,Zthrust+1};

