Part Feature: Difference between revisions

From FreeCAD Documentation
(Hidden properties Data)
(This basic Part::Feature doesn't have a Proxy object so it can't be fully used for sub-classing. Therefore, for Python scripting, the recommendation is to create the Part::FeaturePython object.)
Line 112: Line 112:
doc = App.newDocument()
doc = App.newDocument()
obj = App.ActiveDocument.addObject("Part::Feature", "Name")
obj = App.ActiveDocument.addObject("Part::Feature", "Name")
obj.Label = "Custom label"
}}
<translate>

This basic {{incode|Part::Feature}} doesn't have a Proxy object so it can't be fully used for sub-classing.

Therefore, for [[Python|Python]] scripting, the recommendation is to create the {{incode|Part::FeaturePython}} object.
</translate>
{{Code|code=
import FreeCAD as App

doc = App.newDocument()
obj = App.ActiveDocument.addObject("Part::FeaturePython", "Name")
obj.Label = "Custom label"
obj.Label = "Custom label"
}}
}}
Line 117: Line 130:


<!--T:19-->
<!--T:19-->
* The first argument indicates the type of object, in this case, {{incode|"Part::Feature"}}.
* The first argument indicates the type of object, in this case, {{incode|"Part::FeaturePython"}}.
* The second argument is a string that defines the {{incode|Name}} attribute. If it is not provided, it defaults to {{incode|"Part__Feature"}}.
* The second argument is a string that defines the {{incode|Name}} attribute. If it is not provided, it defaults to {{incode|"Part__FeaturePython"}}.
** The {{incode|Name}} is fixed at creation time; it cannot be modified afterwards.
** The {{incode|Name}} is fixed at creation time; it cannot be modified afterwards.
** The {{incode|Name}} can only include simple alphanumeric characters, and the underscore, {{incode|[_0-9a-zA-Z]}}. If other symbols are given, these will be converted to underscores; for example, {{incode|"A+B:C*"}} is converted to {{incode|"A_B_C_"}}.
** The {{incode|Name}} can only include simple alphanumeric characters, and the underscore, {{incode|[_0-9a-zA-Z]}}. If other symbols are given, these will be converted to underscores; for example, {{incode|"A+B:C*"}} is converted to {{incode|"A_B_C_"}}.

Revision as of 01:34, 17 December 2019

Introduction

A Part Feature object, or formally a Part::Feature, is a simple element with a topological shape associated to it that can be displayed in the 3D view.

Simplified diagram of the relationships between the core objects in the program. The Part::Feature class is the origin of most 2D (Draft, Sketcher) and 3D (Part, PartDesign) objects.

How to use

The Part Feature is an internal object, so it cannot be created from the graphical interface, only from the Python console as described in the Scripting section.

The Part::Feature is defined in the Part Workbench but can be used as the base class for scripted objects in all workbenches that produce 2D and 3D geometrical shapes. For example, it is the parent class of the PartDesign Body, and of the Part Part2DObject, which is specialized for 2D (planar) shapes.

A Part::Feature has simple properties like a placement, and visual properties to define the appearance of its vertices, edges, and faces. Workbenches can add more properties to this basic element to produce an object with complex behavior.

Properties

A Part Feature (Part::Feature class) is derived from the basic App GeoFeature (App::GeoFeature class), therefore it shares all the latter's properties.

In addition to the properties described in App GeoFeature, the Part Feature has the DataShape property, which controls what type of geometry is shown in the 3D view.

See Property for all property types that scripted objects can have.

These are the properties available in the property editor. Hidden properties can be shown by using the Show all command in the context menu of the property editor.

Data

Base

  • DataPlacement: the position of the object in the 3D view. The placement is defined by a Base point (vector), and a Rotation (axis and angle). See Placement.
    • DataAngle: the angle of rotation around the DataAxis. By default, it is (zero degrees).
    • DataAxis: the unit vector that defines the axis of rotation for the placement. Each component is a floating point value between 0 and 1. If any value is above 1, the vector is normalized so that the magnitude of the vector is 1. By default, it is the positive Z axis, (0, 0, 1).
    • DataPosition: a vector with the 3D coordinates of the base point. By default, it is the origin (0, 0, 0).
  • DataLabel: the user editable name of this object, it is an arbitrary UTF8 string.

Hidden properties Data

  • DataExpression Engine: a list of expressions. By default, it is empty [].
  • DataLabel2: a longer, user editable description of this object, it is an arbitrary UTF8 string. By default, it is an empty string "".
  • DataProxy: a class associated with this object.
  • DataShape: a Part TopoShape class associated with this object.
  • DataVisibility: whether to display the object or not.

View

Most objects in FreeCAD have what is called a "view provider", which is a class that defines the visual appearance of the object in the 3D view, and in the tree view. The default view provider of Part Feature objects defines the following properties. Scripted objects that are derived from Part Feature will have access to these properties as well.

Base

  • ViewAngular Deflection: it is a companion to ViewDeviation. It is another way to specify how finely to generate the mesh for rendering on screen or when exporting. The default value is 28.5 degrees, or 0.5 radians. The smaller the value the smoother the appearance will be in the 3D view, and the finer the mesh that will be exported.
  • ViewBounding Box: if it is true, the object will show the bounding box in the 3D view.
  • ViewDeviation: it is a companion to ViewAngular Deflection. It is another way to specify how finely to generate the mesh for rendering on screen or when exporting. The default value is 0.5. The smaller the value the smoother the appearance will be in the 3D view, and the finer the mesh that will be exported.

The formula to calculate this is

deviation in mm = (w + h + d)/3 * value_in_percent/100

where w, h, d are the bounding box dimensions.

  • ViewDisplay Mode: Flat Lines (regular visualization), Shaded (soft edges), Wireframe (no faces), Points (only vertices).
  • ViewDraw Style: Solid, Dashed, Dotted, Dashdot; defines the style of the edges in the 3D view.
  • ViewLighting: Two side, One side; the illumination comes from two sides or one side in the 3D view.
  • ViewLine Color: a tuple of three values (r,g,b) to define the color of the edges in the 3D view.
  • ViewLine Width: a float that determines the width in pixels of the edges in the 3D view.
  • ViewOn Top When Selected: Disabled, Enabled, Object, Element.
  • ViewPoint Color: a tuple of three values (r,g,b) to define the color of the vertices in the 3D view.
  • ViewPoint Size: a float that determines the size in pixels of the vertices in the 3D view.
  • ViewSelectable: if it is true, the object can be picked with the pointer in the 3D view. Otherwise, the object cannot be selected until this option is set to true.
  • ViewSelection Style: Shape, BoundBox.
  • ViewShape Color: a tuple of three values (r,g,b) to define the color of the faces in the 3D view.
  • ViewShow In Tree: if it is true, the object appears in the tree view. Otherwise, it is set as invisible.
  • ViewTransparency: a float from 0 to 100 that determines the level of transparency of the faces in the 3D view. A value of 100 indicates completely invisible faces; the faces are invisible but they can still be picked as long as ViewSelectable is true.
  • ViewVisibility: if it is true, the object appears in the 3D view; otherwise it is invisible. By default this property can be toggled on and off by pressing the Space bar in the keyboard.

Scripting

See also: FreeCAD Scripting Basics, and scripted objects.

A Part Feature is created with the addObject() method of the document.

import FreeCAD as App

doc = App.newDocument()
obj = App.ActiveDocument.addObject("Part::Feature", "Name")
obj.Label = "Custom label"

This basic Part::Feature doesn't have a Proxy object so it can't be fully used for sub-classing.

Therefore, for Python scripting, the recommendation is to create the Part::FeaturePython object.

import FreeCAD as App

doc = App.newDocument()
obj = App.ActiveDocument.addObject("Part::FeaturePython", "Name")
obj.Label = "Custom label"
  • The first argument indicates the type of object, in this case, "Part::FeaturePython".
  • The second argument is a string that defines the Name attribute. If it is not provided, it defaults to "Part__FeaturePython".
    • The Name is fixed at creation time; it cannot be modified afterwards.
    • The Name can only include simple alphanumeric characters, and the underscore, [_0-9a-zA-Z]. If other symbols are given, these will be converted to underscores; for example, "A+B:C*" is converted to "A_B_C_".
    • The Name must be unique in the entire document. If more objects with the same name are created, a sequential number will be appended to distinguish them, for example, "Name001", "Name002", etc.
  • If desired, the Label attribute can be changed to a more meaningful text.
    • By default, the Label is equal to the Name.
    • Unlike the Name, the Label can accept any UTF8 string, including accents and spaces. Since the tree view displays the Label, it is a good practice to change the Label to a more descriptive string.
    • By default the Label must be unique. This behavior can be changed in the preferences editor, Edit → Preferences → General → Document → Allow duplicate object labels in one document.