 Hey, my name is Matt. I'm not sure why I'm making this For some reason I feel compelled to make this video series on CAD from scratch. I Know a lot of people who are really good at CAD And I got a lot of people who are really good at programming things from scratch But not too many are good at both. I think I'm decent at both and so I wanted to take this on as a little project So what I want to do is I want to make a bunch of CAD Utilities what is CAD CAD stands for computer aided design? So I want to make a bunch of tools that can be used to turn ideas Into some kind of physical products. So either from a 3d printer waterjet Some kind of fab shop or just design in general. I want to make black box basically and now a lot of Tools exist for this already. I made a little tier list here. These are all the ones that I've used in the past This is most of them most of the big names at least advocate assault works 360 katia nx free cad Obviously I think most of them are absolutely garbage tier why put them down here Advocates always had a special place in my heart. It's Not really a CAD tool as much as these guys are but it has some bare bones CAD utilities in there and It's mostly for a structural analysis, but it's got some No bells and whistles added, you know programmatic CAD tools that are really really good And I think they're better than all these you know combined. So I've always liked abacus. The problem with abacus is that it it's super expensive You can get a cracker version. I don't advocate that but you can but I will I will say abacus It's not for poor people with with ethics. So What I want to do is make some You know DIY versions of those tools That are kind of like B tier give or take that can do some of the same stuff abacus can Better than these guys down here, but um, you know, not as professional as as abacus and things like that So also, you know free CAD. I will say free CAD is open source and free Whereas the rest of them are proprietary and and quite expensive sometimes We would making tools that are obviously free, but then public domain. So once one step above free CAD in terms of freedoms And so in this video, I want to talk about introduction Which I just did and then also some of the basics of STL STL is a file type that you'll see a lot in 3d printing And other kinds of CAD work. It's just in encodes geometries. Basically here. I have The Wikipedia page it's stereo lithography as STL stands for or no doesn't sense for a standard triangle language and Basically, there's two kind of versions. There's ASCII which is text and then there's binary which is numbers and we will encode For this time being just some ASCII We'll encode some some basic geometry and you know in code and we'll see how it works And to do that can put this kind of very simple model. It's a it's a house It's got a one-by-one cube on the bottom and then a half inch tall pyramid on top Total of nine nodes zero through eight and the node zero is at the origin over here. So I Guess we'll try to code this up and we'll start from there That'll be the first video is coding up this Pretty simple model and this basically sets the stage for doing more advanced things From scratch as well. I mean if you can encode this geometry from scratch you can encode many geometries I mean the average thing that people print is not very complicated You know you make print out a clip you might print out a box print out a Hook or a stand, you know things that people print are not too advanced. So this is not, you know out of the ordinary So I guess we'll get started I got an empty directory here. We'll do everything in C Why because I think C is better. So because we're gonna be writing a An STL file we'll have to include STIO Now we'll need a main function in the main function. We will Basically the first thing we'll do is encode the geometry So we'll need a double to encode the actual nodes. So what I want to do is I want to have an array of nodes There's nine nodes each with three coordinates and Then there's for triangles. I'll use an int array. I think there are 14 triangles, right? There's two triangles for every square face All right Not very neat. Yeah, every square face has two triangles. I Guess we'll do this one Yeah, and then on top the four triangle, so it's 14 triangles and there's again, there's three corners per triangle and Yes, we'll start there. So for the nodes node zero is at zero zero zero Node one is that one zero zero node two is that one one zero and node three is that zero one zero Then nodes four through seven are at the same coordinates only up one level. So one for the z value and then coordinate number eight is at Point five point five point five Yep, and that's what we had to do Double by the way because we have some non integer values now for the triangles themselves Basically what I want is I want to code which vertices are in which triangle. So the first triangle here I'll say is the triangle zero one five Then we have a triangle zero five four And then we have triangle one two six Then we have triangle one six five two three seven two seven six three zero four three four seven Now we're on the the roof four five eight five six eight Not a very fast type of six seven eight And then seven four eight Okay Now we'll have to define And I guess what we'll do is we'll just write a STL now we know that we have everything Defined we'll write in STL. We'll pass in the number of nodes. We'll pass in the number of triangles We'll pass in the actual arrays themselves nodes and triangles and we'll pass in file name so That's going to be a car star File name and I guess we'll name the file house STL. It looks like a pretty good house to me And we'll pass in the address Of that array So that's that's our main function Now we'll have to write the actual write STL function. So we'll have an int for number of nodes int for number triangles We'll have to do it. I think it's like this right double nodes three then int Triangles Three I Think and then it's going to be File name return nothing. It's a void and so I guess we'll get started So first thing we'll do to write the STL is we'll open a file. So we'll say file F equals F open and Then we'll pass in the file name and then we'll pass in this w that means we're going to write to that file name Now we have to look at the actual Format here. So an ASCII STL. Let me zoom in Start stuff with the word solid then a name. I think the word name is optional Yeah, you can omit the the word name if you have a space. So This is how you print to a file you do F print F We'll print out solid quotes solid Space and then we'll look at new line and then at the very end of the file We'll put in solid. So basically that's the first line in the last minute of the ASCII STL done Now you have to do the inside parts. So Once we have our solid we have to go through the faces of the triangle so I do that we have to iterate through the number of triangles so we'll say four in I equals zero I less than None triangles I Plus plus now in the for each triangle basically We'll first need a normal a normal vector so A normal is pretty simple. We could equate it manually. So Basically each of these triangles here for example this triangle here The normal should be pointing outwards To get that vector. What we'll do is we'll take the vector from zero to one and the vector from zero to five You got a cross product of those and that should give us this vector here this outward vector for every triangle We could program it manually. It's not too hard But I mean we could write it down manually, but it's not too hard to program. So we'll make an actual function for this We'll say Get normal We'll pass in the coordinates of points zero one and five or in general just be point one point two and point three So we'll say double point one Size three double point two size three double Point three size three and then we'll pass in a an output Array as well. So double normal size three Such three I showed Good Again you return nothing So we'll define the two vectors. So basically we just passed in location of point zero point one and point five on this triangle You don't have a vector though. So we'll have to define a vector. We'll say double v1 size three pull equal P2 zero minus P1 zero copy that three times so this is basically subtracting the the X Y and Z coordinates of point one and two We'll do the same thing with points one and three. It's changing the three to two to three I should say point three this should be a two and This should be called the vector two now cross product. Basically what a cross product is is just the Multiplication of the terms not on that axis Subtracted if that makes any sense. You can look up what a cross product is. I'll just define it manually here. So The first component of the normal will be let's see v1 1 times v2 to Minus the backwards of those two so v1 2 times v2 1 and then the other two components Are the same thing just with I think that's gonna be two zero Zero two The last one would be zero one one zero That should be right. If not, we'll figure out pretty quick so Yeah, this will define normal normal and that will be output automatically That's good. So now we now if we call that function So we'll do we'll define double normal size three And I will call the phone supposed to get normal Then we'll pass in the three points The points are going to be hard to define because they're going to be you know inside the triangle array In the node array. So we'll have to look through each triangle Find the zero node and then pass, you know those points as The first coordinate and then we'll pass the vertex one coordinate as the second parameter and then the third vertex as the third parameter so it's not too easy, but I guess it'll be Nodes Triangles I And then we'll do zero for the first one. Yeah Don't do the same thing the other two. Let's do that This will be second and first component and we'll also have to pass in the normal array That will give us Basically the normal of each triangle. So for the first one, it'll be this normal then it will be this normal Then it will be this normal. It will be this normal. I'll go through the whole House here and give us all the normals one by one now we can print out the actual These two lines I guess so we'll say f print f Into f will print out What face it's normal Person after son after son that because we're gonna pass in three values and we will pass in normal zero Normal one Normal two and now we'll print out this thing here outer loop So we'll do oh We forgot a new line might be So tab outer loop New line We'll print out at the end. We'll put end loop And now inside that in those two if you look back at the wiki We have to print out these three things three three vertices pretty easy. So we'll say four int j equals zero j less than Three j plus plus obviously you don't have to look through this you can just do you know Them all one by one that will do we'll look through it Well f print f two tabs the word of vertex then Person after son after son f new line And I have put in the the three coordinates of each vertex in the triangle not too hard. Well, I guess we'll say nodes Triangles I J Zero hold on I j zero. We'll copy this one. Let me fix that again as usual So this will give us the x-coordinate. This won't give us the y-coordinate This one will give us the z-coordinate. Yep, and We'll wrap up that loop that should print out everything here. I think that's it. I think we're done So if we compile this Hey, wow no errors. That's ridiculous and now we can run a dot out That should give us an STL Let's open the STL. Here's the STL It looks reason hold on the last one looks like it's broken Let me take a look real quick Okay, oops. Yeah might be so basically what I did was I forgot to put in two of the triangles So I didn't put in these these two bottom triangles here. So zero three two and zero two one So we'll put those in Zero three two Zero Two one Right. Yeah Now if I recompile that Now I should have all 14. Yeah all 14 and they point down good So now I have a pretty large file. It's 2.4k just for this do-below house, but we have an STL file and just to prove to you that it's real Let's open up a STL viewer So we'll drag in our STL There you go. Here's our STL. We coded this whole thing from scratch Not hard took a while. It's been like, you know 20 minutes But that was me talking for a good point a good part of it. So yeah, there you go. There's our house in a STL format Not hard at all So in the next video, I wanted to Do a binary STL so convert this to Sort of the more efficient Number based encoding then also make a little bit of a viewer So we can see this without having to use some online tool like this one Anyway, if you enjoyed the video leave a like and Have a good day