
SetFactory("OpenCASCADE");

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

symmetry = 0;

ri=0.1115;
re=0.1575;
rcat=0.0248;
rgrafite=0.095;
rceramic=0.175;
rsphere = 0.10;
ds=0.007;
rbox=0.22;
dbox=0.25;
inter=0.0; // 0.5;
larm=2.5-0.543;
lcyl=0.10;
Lboom = 1.93;
Rarm=0.1;
Rarm1 = 0.1;
Rarm2 = 0.04;
gamma = 0;   // rotation around X
Teta=26;     // Rotation around Y
Psi = 0;     // Rotation around Z
Beta=90;     // Panel rotation
TetaFF=0;    // Far Field rotation
Hsat=8.9;
Wsat=2.8;
Dsat=2.4;
Zthrust=-(Hsat/2.0-2.50);

Lpanel=20;
Hpanel=8.1;
Wpanel = 0.03;
Xpanel=Wsat/2+5.6;
Zpanel=Hsat/2.0-1.450;

Rff=8;
Lff = Xpanel+Lpanel-Rff/2.0+1;
Zff=Zthrust;
If (Teta == 0) 
   Teta = 0.001;
EndIf

Tetadeg = Teta;

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


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};
Circle(nl+7) = {-0, -inter/2, 0, rbox, 0, 2*Pi};

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

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};
Curve Loop(ll+7) = {nl+7};

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};
Plane Surface(ns+5) = {ll+7, ll+6};

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

Extrude {-dbox, 0, 0} {Curve{nl+7};}

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

Delete {
  Volume{1}; 
}
Delete {
  Surface{19}; 
}

ll = newll;
ns = news;

Curve Loop(ll) = {12};
Curve Loop(ll+1) = {13};
Plane Surface(ns) = {ll, ll+1};

Surface Loop(2) = {20, 18, 23, 17, 14, 13, 16, 12, 15, 11, 10, 9};
Volume(1) = {2};

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

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

/// ROTATE AROUND Y

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


/// ROTATE AROUND Z

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

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

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

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

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

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

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

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

BooleanDifference{ Volume{4}; Delete; }{ Volume{1}; }

Delete {
  Volume{1}; Volume{4}; 
}

Recursive Delete {
  Surface{19}; Surface{32}; 
}

BooleanFragments{ Surface{20}; Delete; }{ Surface{31}; }
//+
Recursive Delete {
  Surface{33}; 
}

Surface Loop(5) = {22, 21, 32, 31, 23, 24, 25, 26, 27, 28, 29, 30};

Volume(3) = {5};
Cone(4) = {Wsat/2, 0, Zthrust, larm+0.01, 0, 0, Rarm1, Rarm2, 2*Pi};

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

BooleanDifference{ Volume{4}; Delete; }{ Volume{3}; }

Delete {
  Volume{2};Volume{3}; Volume{4}; 
}

Delete {
  Surface{35}; Surface{14}; 
}

Curve Loop(63) = {22, -21, -20, 23};

nsurf = 52;

If (Tetadeg > 37)
   nsurf = 49;
EndIf

Curve Loop(64) = {nsurf};
Plane Surface(35) = {63, 64};
Surface Loop(6) = {35, 16, 17, 13, 15, 18, 33, 34};

BooleanDifference{ Surface{31}; Delete; }{ Surface{34}; Delete; }
Surface Loop(7) = {35, 16, 17, 13, 15, 18, 33, 31, 32, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};

Volume(1) = {7};
Fillet{1}{25, 21, 27, 26, 23, 24}{0.2}

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

p1[] = Point{9999};
p2[] = Point{9998};

p3[0] = p1[0]-p2[0];
p3[1] = p1[1]-p2[1];
p3[2] = p1[2]-p2[2];

mod = Sqrt(p3[0]*p3[0]+p3[1]*p3[1]+p3[2]*p3[2]);

p3[0] = p3[0] / mod;
p3[1] = p3[1] / mod;
p3[2] = p3[2] / mod;

Printf("center #1 = (%f,%f,%f)",p1[0],p1[1],p1[2]);
Printf("normal = (%f,%f,%f)",p3[0],p3[1],p3[2]);

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

nv = newv;

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

/// ROTATE PANEL

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

/// FAR FIELD --------------------------------------

nv=newv;

Cylinder(501) = {0, 0, Zff, Lff, 0, 0, Rff, 2*Pi};
Sphere(502) = {Lff, 0, Zff, Rff, -Pi/2, Pi/2, 2*Pi};

BooleanUnion{ Volume{501}; Delete; }{ Volume{502}; Delete; }

Translate {0, 0, 2} {
  Volume{501}; 
}

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

Merge "merge_1.geo";

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

Merge "merge_2.geo";

Curve Loop(108) = {503:514};

ns = news;
Plane Surface(ns) = {108};
Extrude {0, Wpanel, 0} {
  Surface{ns}; 
}

Fillet{502}{516,522,528,534,515,524,526,536}{0.3}

nv=newv;

Cylinder(503) = {Xpanel-Lboom-1, 0, Zpanel, 1.0+0.23, 0, 0, 0.12, 2*Pi};
//Cylinder(503) = {Xpanel-Lboom-1, 0, Zpanel, 2.0+0.23, 0, 0, 0.12, 2*Pi};

BooleanUnion{ Volume{503}; Delete; }{ Volume{502}; Delete; }

BooleanUnion{ Volume{502}; Delete; }{ Volume{500}; Delete; }


//BooleanUnion{ Volume{66}; Delete; }{ Volume{63}; Delete; }
Rotate {{1, 0, 0}, {Xpanel, 0.0, Zpanel}, Beta} {Volume{502};}

BooleanDifference{ Volume{501}; Delete; }{ Volume{502}; Volume{1}; Delete; }

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

Physical Surface("S/C", 1000) = {1,3,4,7,8,10:13,73,74};

Physical Surface("Inner Pole", 1001) = {24,25};
Physical Surface("Thruster", 1002) = {16:18};
Physical Surface("Symmetry", 1003) = {70};
Physical Surface("Panel #1", 1004) = {37};
Physical Surface("Panel #2", 1005) = {39,40,41,42,36,44,45};
Physical Surface("Boom", 1006) = {50,51,67,68};
Physical Surface("Boom 2", 1007) = {57, 47, 43, 54, 55, 56, 
                                    38, 46, 58, 53, 52, 59,
                                    60, 49, 48, 66, 61, 65, 64, 62, 63};
Physical Surface("FarField", 1008) = {71, 72, 69};
Physical Surface("Ceramics", 1009) = {19,20,22,23};
Physical Surface("Channel", 1010) = {21};
Physical Surface("Thruster Arm", 1011) = {6,14,15};
Physical Surface("S/C side", 1012) = {2,5};
//Physical Surface("S/C side", 1000) += {2,5};

Physical Surface("Antenna #1", 1013) = {81:83};
Physical Surface("Antenna #2", 1014) = {84:86};

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

Show "*";
//+
Hide {
Point{10005,10006,10011,10012,10014,10015,10016,10017,10102,10103,10104};
Curve{8,17,20,22,24,25,26,27,162,163,164,165,166,167,168,169,170};
Surface{69,70,71,72};
}

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

Transfinite Curve {115, 140, 110, 135, 106, 106, 131, 102, 128} = 6 Using Progression 1;
Transfinite Curve {25, 12, 14, 22, 24, 9, 11, 20} = 4 Using Progression 1;

Delete {
  Volume{501}; Surface{1}; 
}

Point(600)={Wsat/2, -0.5, Zthrust-0.5, 1.0};
Point(601)={Wsat/2,  0.5, Zthrust-0.5, 1.0};
Point(602)={Wsat/2, -0.5, Zthrust+0.5, 1.0};
Point(603)={Wsat/2,  0.5, Zthrust+0.5, 1.0};

Line(171) = {603, 601};
Line(172) = {601, 600};
Line(173) = {600, 602};
Line(174) = {602, 603};

Curve Loop(141) = {4, 3, -1, -2};
Curve Loop(142) = {174, 171, 172, 173};
Plane Surface(73) = {141, 142};
Curve Loop(143) = {174, 171, 172, 173};
Curve Loop(144) = {5};
Plane Surface(74) = {143, 144};

Surface Loop(7) = {5, 73, 4, 12, 70, 69, 71, 72, 2, 8, 
                   13, 7, 11, 3, 10, 74, 6, 14, 15, 16, 
                   17, 18, 19, 20, 21, 22, 23, 24, 25};
Surface Loop(8) = {37, 40, 36, 38, 43, 48, 49, 47, 50, 
                   51, 52, 53, 54, 46, 67, 68, 39, 41, 
                   42, 45, 64, 65, 66, 44, 63, 62, 61, 
                   60, 59, 58, 57, 56, 55};

Volume(1) = {7, 8};

Transfinite Curve {174, 173, 172, 171} = 4 Using Progression 1;
//Physical Volume("Volume", 1012) = {1};

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

Transfinite Surface {12};
Transfinite Curve {29, 16} = 7 Using Progression 1;
Transfinite Curve {7, 23, 6, 21, 15, 28} = 7 Using Progression 1;
Transfinite Curve {2, 10, 13, 3} = 11 Using Progression 1;
Transfinite Surface {4};
Transfinite Surface {8};
Transfinite Surface {10};
Transfinite Surface {3};
Transfinite Surface {7};

////////// ANTENNE //////////////////////////

Sphere(2) = {0, 0, 0, 20, -Pi/2, Pi/2, 2*Pi};
Sphere(3) = {0, 0, 0, 19.96, -Pi/2, Pi/2, 2*Pi};
BooleanDifference{ Volume{2}; Delete; }{ Volume{3}; Delete; }

Cylinder(3) = {0, 0, 0, 0, 0, 24, 2.2, 2*Pi};

BooleanIntersection{ Volume{2}; Delete; }{ Volume{3}; Delete; }

Translate {3.5,3, -14} {
  Volume{2}; 
}

Translate {0, -6, 0} {
  Duplicata { Volume{2}; }
}
Rotate {{1, 0, 0}, {3.5, 3, 6}, -4*Pi/180} {
  Volume{2}; 
}

Rotate {{1, 0, 0}, {3.5,-3, 6}, 4*Pi/180} {
  Volume{3}; 
}

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

Surface Loop(13) = {69, 71, 72, 70, 2, 73, 5, 12, 13, 8, 4, 10, 3, 11, 7, 74, 6, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};
Surface Loop(14) = {37, 40, 36, 38, 43, 48, 49, 47, 50, 51, 52, 53, 54, 46, 67, 68, 39, 41, 42, 45, 64, 65, 66, 44, 63, 62, 61, 60, 59, 58, 57, 56, 55};
Surface Loop(15) = {83, 82, 81};
Surface Loop(16) = {86, 85, 84};

Volume(1) = {13, 14, 15, 16};

If (symmetry == 1) 

Symmetry {1, 0, 0, 0} {
  Duplicata { Surface{83}; Surface{81}; Surface{82}; Surface{85}; Surface{86}; Surface{84}; Surface{50}; Surface{43}; Surface{66}; Surface{65}; Surface{64}; Surface{63}; Surface{62}; Surface{61}; Surface{57}; Surface{54}; Surface{46}; Surface{38}; Surface{56}; Surface{55}; Surface{44}; Surface{45}; Surface{36}; Surface{40}; Surface{42}; Surface{37}; Surface{39}; Surface{41}; Surface{2}; Surface{11}; Surface{5}; Surface{73}; Surface{10}; Surface{3}; Surface{7}; Surface{74}; Surface{6}; Surface{13}; Surface{12}; Surface{4}; Surface{8}; Surface{14}; Surface{15}; Surface{16}; Surface{17}; Surface{20}; Surface{25}; Surface{22}; Surface{21}; Surface{19}; Surface{23}; Surface{24}; }
}

Physical Surface("ant 3", 1015) = {91, 92, 90};
Physical Surface("ant 4", 1016) = {87, 88, 89};
Physical Surface("S/C side", 1012) += {117, 115};
Physical Surface("S/C", 1000) += {116, 119, 120, 121, 125, 127, 124, 118, 122};
Physical Surface("Panel 2", 1017) = {112};
Transfinite Curve {477, 507, 483, 483, 487, 515, 467, 511, 473} = 6 Using Progression 1;

EndIf
Physical Volume("Volume", 1015) = {1};

Show "*";
//+
Hide {
Point{10102,10103,10104};
Curve{162,163,164,165,166,167,168,169,170};
Surface{69,71,72};
}

Transfinite Curve {77, 75} = 49 Using Progression 1;
Transfinite Curve {76} = 25 Using Progression 1;
Transfinite Curve {81, 71} = 7 Using Progression 1;
Transfinite Curve {79} = 11 Using Progression 1;
Transfinite Curve {78, 80} = 2 Using Progression 1;
Transfinite Surface {37} = {10050, 10049, 10048, 10044};
Transfinite Curve {115, 110, 110, 135, 106, 106, 102, 131, 128, 140} = 8 Using Progression 1;
