B4J Question try to make a simple cad...

trvip

Member
Hi

I'm trying to make a drawing program (simple CAD).
Everything I draw is saved in the shapes list and then displayed in the canvas.
i took the erel program as a template and changed something.
There are 3 classes (Line, Rectangle, Circle).

But now I have the following problem: how can I, for example, select a line or a rectangle and delete it, change it?
maybe it's better if i have 3 list for the shapes (one for line, one for rectangle, ...)???

Thanks in advance
 

Attachments

  • myDrawX.zip
    2.9 KB · Views: 104

Brian Dean

Well-Known Member
Licensed User
Longtime User
I have the same idea
Several years ago (I am talking last century here) there was a simple CAD tool called "Designer". It was very easy and pleasant to use and could be used for a small home project or for something much more extensive - I used it to plan out an entire factory production floor. Unfortunately it was not updated to work with more recent Windows versions and I have never found anything that comes near to replacing it. It would be very nice if somebody would create one.

But I think it's difficult.
I agree. Don't think that making something simple is an easy job. You need to be very clever to make things simple - Erel and B4X are an excellent example, in my opinion.

A form designer is a simple CAD system - when you start to handle angled lines and arcs things soon stop being simple. And beyond that there is zoom and pan. You need to keep all of your point data in an index so that you can relate the current cursor position to the closest drawn object. This needs a two-dimensional search but you can find those in Wikipedia. You need to have at least two drawing layers (canvases). One of these will be the "active" layer where you can manipulate a line object before returning it to an underlying layer. What I am trying to say here is that you need to have a well-thought out system design before you start any coding.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
But I think it's difficult.
Yes, it is not an easy task.

First of all, you need to define a good data structure.
You have Points, Lines, PolyLines, Rectangles, Polygon etc.
A Point has two coordinates.
A Line has or four coordinates or two Points.
If you use two Points to define a Line you should use indexes for the Points and not directly their coordinates.
A same single Point can belong to more than one line, and if you modify it, all parent object are modified.
All this needs to be taken into account, and it is only the short story.

Then you need a physical coordinate system with a scale to transform physical coordinates to screen coordinates.

Then, some comments to your program.
I remember having seen Erels program, but is a relatively ancient one, lots of things have changed in the meantime.
- Fist suggestion is to switch to B4XPages, and use B4XObjects like B4XView, B4XCanvas etc.
But, maybe you could stay with B4J only, in that case you could have a look at the jCanvasExt libary which extends the standard Canvas.
- You define the user interface in the code, you should do it in the Designer.
- Add a third Canvas for the cursor to show it on the screen with a cross and display its coordinates.
- In the Touch event routine you should

Then to select any object you need to check if the cursor is close to one of its components.
For a Point it is easy, go through all shapes and check for each point if the difference between its coordinates and the cursor coordinates is smaller than the snap distance.
For a line you need to check if the cursor is in the rectangle formed by the two points and then if the distance between the cursor and the line is smaller than the snap distance.
For a shape it depends on its components and on the data structure.
Searching for a circle is different from searching for a rectangle.
For a circle you need to check if the cursor is either near to the center or check if the distance between the cursor and the center is close to the circle radius.
For a rectangle you must check the four points and the four lines.
Etc, etc...

Unfortunately, for the search functions the current data structure is not appropriate.
Impossible to know the shape type nor get the coordinates.

This means that you need to first define the right data structure.
When you have done it i could help further.

It is a long story, explaining some subjects of the problem, I can only encourage to go further.
 
Last edited:
Upvote 0

yzhy-mail

Member
I

Yes, it is not an easy task.

First of all, you need to define a good data structure.
You have Points, Lines, PolyLines, Rectangles, Polygon etc.
A Point has two coordinates.
A Line has or four coordinates or two Points.
If you use two Points to define a Line you should use indexes for the Points and not directly their coordinates.
A same single Point can belong to more than one line, and if you modify it, all parent object are modified.
All this needs to be taken into account, and it ls only the short story.

Then you need a physical coordinate system with a scale to transform physical coordinates to screen coordinates.

Then, some comments to your program.
I remember having seen Erels program, but is a relatively ancient one, lots of things have changed in the meantime.
- Fist suggestion is to switch to B4XPages, and use B4XObjects like B4XView, B4XCanvas etc.
But, maybe you could stay with B4J only, in that case you could have a look at the jCanvasExt libary which extends the standard Canvas.
- You define the user interface in the code, you should do it in the Designer.
- Add a third Canvas for the cursor to show it on the screen with a cross and display its coordinates.
- In the Touch event routine you should

Then to select any object you need to check if the cursor is close to one of its components.
For a Point it is easy, go through all shapes and check for each point if the difference between its coordinates and the cursor coordinates is smaller than the snap distance.
For a line you need to check if the cursor is in the rectangle formed by the two points and then if the distance between the cursor and the line is smaller than the snap distance.
For a shape it depends on its components and on the data structure.
Searching for a circle is different from searching for a rectangle.
For a circle you need to check if the cursor is either near to the center or check if the distance between the cursor and the center is close to the circle radius.
For a rectangle you must check the four points and the four lines.
Etc, etc...

Unfortunately, for the search functions the current data structure is not appropriate.
Impossible to know the shape type nor get the coordinates.

This means that you need to first define the right data structure.
When you have done it i could help further.

It is a long story, explaining some subjects of the problem, I can only encourage to go further.
That's so kind of you !
May be someone will try it !
 
Upvote 0
Top