Jul-08-2020, 02:26 AM
I want to write a script that generates a list of (X,Y) coordinates to be passed into CAD software in order to automate some extremely repetitive geometry-related tasks. I am learning python on my own as my first OOP language. I have no formal background in engineering or computer programming. I used to be pretty good with BASIC back in the MSDOS days, but I've never worked with any of the modern languages. I'm doing ok so far, but I have run into an issue where I need to generate some objects, the number of which is defined by the user. I need a hierarchical data structure in which data can be individually recalled later by a naming convention that can be programmed - an array of sorts. My first instinct was to have variables created in a FOR loop that would generate sequential names, such as var1, var2, etc. but upon my research it seems that this practice is not only frowned upon, but also requires some hacky low-level coding like using vars(), which can be problematic in some situations.
Here is an example of what I am trying to do:
The user is asked to input height, width, and qty of rectangles (boxes). The script generates (X,Y) coordinates for a row of equally sized and equally spaced boxes. Later in the code, I want to be able to refer to specific boxes for further processing. The way it is written currently, if I need to refer to a specific point I can do something like print(Box1.A.X) to get the X coordinate of the bottom left corner of the first rectangle. I need to be able to reference the data at all levels of the hierarchy. In other words I need to be able to call an individual box as a list of(A,B,C,D), individual point as a list of (X,Y), or an individual X or Y as an integer.
This is how I have gone about it so far:
I created a class called Point and gave it attributes self.X and self.Y. When an instance is created, the X and Y values are fed in as arguments. I then created a new class called Box. This object has 2 arguments for the X/Y values of the bottom-left point (A). Then, 4 instances of the Point class are created starting with Point A and adding the height and width to the X and Y values to get B,C, and D. Now, I can create a rectangle easily using something like Box1=Box(3,2). This makes a rectangle with the bottom-left point at (3,2). Then if I need to refer to a specific point I can do something like print(Box1.A.X) to get the X coordinate of the bottom left corner of the first rectangle. What I need now is to be able to reference the data at all levels of the hierarchy, in other words I need to be able to call an individual box as a list of (A,B,C,D), individual point as a list of (X,Y), or an individual X or Y as an integer.
This is where I'm stumped. I want to generate 'n' number of boxes, but every way I've tried to create the name of the new boxes has failed. I can programatically generate a string with the name I want, but apparently strings can't be used for variable names.
I also tried generating a nested dictionary, such that each point object yields a dictionary with keys X and Y, each box object yields a dictionary with keys for points ABCD, and then have a master dictionary with keys for each individual box. When I tried it though, I was able to generate key names for the coordinates and points, but not for the boxes. What is the convention for organizing an array of data like this?
For reference, here is a sample of my code:
Here is an example of what I am trying to do:
The user is asked to input height, width, and qty of rectangles (boxes). The script generates (X,Y) coordinates for a row of equally sized and equally spaced boxes. Later in the code, I want to be able to refer to specific boxes for further processing. The way it is written currently, if I need to refer to a specific point I can do something like print(Box1.A.X) to get the X coordinate of the bottom left corner of the first rectangle. I need to be able to reference the data at all levels of the hierarchy. In other words I need to be able to call an individual box as a list of(A,B,C,D), individual point as a list of (X,Y), or an individual X or Y as an integer.
This is how I have gone about it so far:
I created a class called Point and gave it attributes self.X and self.Y. When an instance is created, the X and Y values are fed in as arguments. I then created a new class called Box. This object has 2 arguments for the X/Y values of the bottom-left point (A). Then, 4 instances of the Point class are created starting with Point A and adding the height and width to the X and Y values to get B,C, and D. Now, I can create a rectangle easily using something like Box1=Box(3,2). This makes a rectangle with the bottom-left point at (3,2). Then if I need to refer to a specific point I can do something like print(Box1.A.X) to get the X coordinate of the bottom left corner of the first rectangle. What I need now is to be able to reference the data at all levels of the hierarchy, in other words I need to be able to call an individual box as a list of (A,B,C,D), individual point as a list of (X,Y), or an individual X or Y as an integer.
This is where I'm stumped. I want to generate 'n' number of boxes, but every way I've tried to create the name of the new boxes has failed. I can programatically generate a string with the name I want, but apparently strings can't be used for variable names.
I also tried generating a nested dictionary, such that each point object yields a dictionary with keys X and Y, each box object yields a dictionary with keys for points ABCD, and then have a master dictionary with keys for each individual box. When I tried it though, I was able to generate key names for the coordinates and points, but not for the boxes. What is the convention for organizing an array of data like this?
For reference, here is a sample of my code:
#USER DEFINED VARIABLES (I just put in some simple defaults for sake of this example) box_qty = 4 box_spacing = 1 box_height = 3 box_width = 6 class Point: """Object containing X-Y coordinates of a single point""" def __init__(self,x,y): self.X = x self.Y = y def __str__(self): return self.__dict__ class Box: """Creates a rectangle and assigns 4 points, ABCD, to it relative to point A""" def __init__(self,x,y): self.A = Point(x,y) self.B = Point(x, y + box_height) self.C = Point(x + box_width, y + box_height) self.D = Point(x + box_width, y) def __str__(self): return f'A:{self.A}\nB:{self.B}\nC:{self.C}\nD:{self.D}'# Now, I need something like this, but written in such a way that actually works

start = Point(0,0) for i in range(box_qty): f'Box{i}' = Box(start.X,start.Y) start.X += (box_width + box_spacing) # ...so that I can write a function such as: def get_box(box_name,point_name,x_or_y): ~some code that parses the name of a specific point get_box(Box1,C,X) >>>6