How to set units during modeling?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
freecadlzh
Posts: 138
Joined: Fri Mar 06, 2020 12:52 pm

How to set units during modeling?

Post by freecadlzh »

I used freecad to build a model of substation.It involves the enclosure of the substation, transformer and other models. The enclosure, house and other models are represented by part's box model.In order not to build the same substation model every time, I stored the data of the substation model and put it into the SQL database.The size of a substation should be about hundreds of meters, so the data stored in the database is also hundreds of dozens of data. As shown in the following figure:
Image

The code for establishing the substation model is basically established by using the box in part. After establishing the model, coordinate transformation is carried out through the parameters entered by the user.

Code: Select all

DeltaX = FreeCAD.Units.Quantity(self.ui.txtCenterPointX.text()).Value#
DeltaY = FreeCAD.Units.Quantity(self.ui.txtCenterPointY.text()).Value#
DeltaZ = FreeCAD.Units.Quantity(self.ui.txtCenterPointZ.text()).Value#
aBox = FreeCAD.activeDocument().addObject("Part::Box",tmpBoxName)
aBox.Width = FreeCAD.Units.Quantity(self.ui.txtWidth.text()).Value#
aBox.Length = FreeCAD.Units.Quantity(self.ui.txtLength.text()).Value#
aBox.Height = FreeCAD.Units.Quantity(self.ui.txtHeight.text()).Value#
The built substation looks like this:

Image
It was recently discovered that the unit of freecad is mm by default.However, I found that units can be modified in preferences.I changed the unit from mm to m in preference, and then returned to freecad and found that the unit of the model did change from mm to m, but the data also changed from 100 to 0.1.
The problem is, in order to avoid repeated modeling, I designed a set of code to rebuild the model after traversing the model, and called different Python codes to rebuild the model according to the properties of the model. For example, now the height data of a fence model in the database is 5, so I think it is 5m high, but when I restore it with code, execute
abox = freecad. Activedocument(). AddObject ("part:: box", tmpboxname) to create a box,
and then
abox Height = freecad.units.quantity (self.ui.txtheight.text()).
The assigned value should be the number 5 read from the database, and when it is displayed in freecad, it will become 0.005M, which is not consistent with my original intention.

A stupid way is that I manually multiply the data in the database by 1000, and then I can import it normally without changing the imported model code. But what if I create a new model? For example, if you build a fence with a height of 6, the database will still save 6 instead of 6000.

So I hope to find a way to use Python scripts, such as abox = freecad Activedocument(). AddObject ("part:: box", tmpboxname) can set the unit when creating a box, so that it can also pass a number of 5 to know whether it is 5m or 5mm. Is there such code?
Attachments
2.jpg
2.jpg (207.81 KiB) Viewed 758 times
1.jpg
1.jpg (593.34 KiB) Viewed 758 times
User avatar
freecadlzh
Posts: 138
Joined: Fri Mar 06, 2020 12:52 pm

Re: How to set units during modeling?

Post by freecadlzh »

freecadlzh wrote: Thu Jul 28, 2022 5:25 am I used freecad to build a model of substation.It involves the enclosure of the substation, transformer and other models. The enclosure, house and other models are represented by part's box model.In order not to build the same substation model every time, I stored the data of the substation model and put it into the SQL database.The size of a substation should be about hundreds of meters, so the data stored in the database is also hundreds of dozens of data. As shown in the following figure:
Image

The code for establishing the substation model is basically established by using the box in part. After establishing the model, coordinate transformation is carried out through the parameters entered by the user.

Code: Select all

DeltaX = FreeCAD.Units.Quantity(self.ui.txtCenterPointX.text()).Value#
DeltaY = FreeCAD.Units.Quantity(self.ui.txtCenterPointY.text()).Value#
DeltaZ = FreeCAD.Units.Quantity(self.ui.txtCenterPointZ.text()).Value#
aBox = FreeCAD.activeDocument().addObject("Part::Box",tmpBoxName)
aBox.Width = FreeCAD.Units.Quantity(self.ui.txtWidth.text()).Value#
aBox.Length = FreeCAD.Units.Quantity(self.ui.txtLength.text()).Value#
aBox.Height = FreeCAD.Units.Quantity(self.ui.txtHeight.text()).Value#
The built substation looks like this:

Image
It was recently discovered that the unit of freecad is mm by default.However, I found that units can be modified in preferences.I changed the unit from mm to m in preference, and then returned to freecad and found that the unit of the model did change from mm to m, but the data also changed from 100 to 0.1.
The problem is, in order to avoid repeated modeling, I designed a set of code to rebuild the model after traversing the model, and called different Python codes to rebuild the model according to the properties of the model. For example, now the height data of a fence model in the database is 5, so I think it is 5m high, but when I restore it with code, execute
abox = freecad. Activedocument(). AddObject ("part:: box", tmpboxname) to create a box,
and then
abox Height = freecad.units.quantity (self.ui.txtheight.text()).
The assigned value should be the number 5 read from the database, and when it is displayed in freecad, it will become 0.005M, which is not consistent with my original intention.

A stupid way is that I manually multiply the data in the database by 1000, and then I can import it normally without changing the imported model code. But what if I create a new model? For example, if you build a fence with a height of 6, the database will still save 6 instead of 6000.

So I hope to find a way to use Python scripts, such as abox = freecad Activedocument(). AddObject ("part:: box", tmpboxname) can set the unit when creating a box, so that it can also pass a number of 5 to know whether it is 5m or 5mm. Is there such code?
To put it simply, I provide a real number, such as 300. I want freecad to think it is 300m, not 300mm. How can this be done in the code?
User avatar
freecadlzh
Posts: 138
Joined: Fri Mar 06, 2020 12:52 pm

Re: How to set units during modeling?

Post by freecadlzh »

freecadlzh wrote: Thu Jul 28, 2022 5:33 am
freecadlzh wrote: Thu Jul 28, 2022 5:25 am I used freecad to build a model of substation.It involves the enclosure of the substation, transformer and other models. The enclosure, house and other models are represented by part's box model.In order not to build the same substation model every time, I stored the data of the substation model and put it into the SQL database.The size of a substation should be about hundreds of meters, so the data stored in the database is also hundreds of dozens of data. As shown in the following figure:
Image

The code for establishing the substation model is basically established by using the box in part. After establishing the model, coordinate transformation is carried out through the parameters entered by the user.

Code: Select all

DeltaX = FreeCAD.Units.Quantity(self.ui.txtCenterPointX.text()).Value#
DeltaY = FreeCAD.Units.Quantity(self.ui.txtCenterPointY.text()).Value#
DeltaZ = FreeCAD.Units.Quantity(self.ui.txtCenterPointZ.text()).Value#
aBox = FreeCAD.activeDocument().addObject("Part::Box",tmpBoxName)
aBox.Width = FreeCAD.Units.Quantity(self.ui.txtWidth.text()).Value#
aBox.Length = FreeCAD.Units.Quantity(self.ui.txtLength.text()).Value#
aBox.Height = FreeCAD.Units.Quantity(self.ui.txtHeight.text()).Value#
The built substation looks like this:

Image
It was recently discovered that the unit of freecad is mm by default.However, I found that units can be modified in preferences.I changed the unit from mm to m in preference, and then returned to freecad and found that the unit of the model did change from mm to m, but the data also changed from 100 to 0.1.
The problem is, in order to avoid repeated modeling, I designed a set of code to rebuild the model after traversing the model, and called different Python codes to rebuild the model according to the properties of the model. For example, now the height data of a fence model in the database is 5, so I think it is 5m high, but when I restore it with code, execute
abox = freecad. Activedocument(). AddObject ("part:: box", tmpboxname) to create a box,
and then
abox Height = freecad.units.quantity (self.ui.txtheight.text()).
The assigned value should be the number 5 read from the database, and when it is displayed in freecad, it will become 0.005M, which is not consistent with my original intention.

A stupid way is that I manually multiply the data in the database by 1000, and then I can import it normally without changing the imported model code. But what if I create a new model? For example, if you build a fence with a height of 6, the database will still save 6 instead of 6000.

So I hope to find a way to use Python scripts, such as abox = freecad Activedocument(). AddObject ("part:: box", tmpboxname) can set the unit when creating a box, so that it can also pass a number of 5 to know whether it is 5m or 5mm. Is there such code?
To put it simply, I provide a real number, such as 300. I want freecad to think it is 300m, not 300mm. How can this be done in the code?
Image
Attachments
3.png
3.png (319.92 KiB) Viewed 709 times
galou_breizh
Posts: 437
Joined: Wed Sep 15, 2010 9:38 am

Re: How to set units during modeling?

Post by galou_breizh »

Code: Select all

DeltaX = float(self.ui.txtCenterPointX.text()) * App.Units.Metre
aBox.Width = DeltaX
or

cf. https://forum.freecadweb.org/viewtopic.php?f=22&t=70358.
Post Reply