Skip to content
Snippets Groups Projects
np_float_poly.py 84.8 KiB
Newer Older
  • Learn to ignore specific revisions
  •     instruct = main
        keys_aff = 'MMB, SCROLL - navigate'
        keys_nav = 'LMB, SPACE - leave navigate'
        keys_neg = 'ESC, RMB - quit'
    
        display_instructions(region, rv3d, instruct, keys_aff, keys_nav, keys_neg)
    
        font_id = 0
    
        # Drawing the small badge near the cursor with the basic instructions:
    
        symbol = copy.deepcopy(NP020FP.polysymbol)
        badge_mode = 'NAV'
        message_main = 'NAVIGATE'
        message_aux = None
        aux_num = None
        aux_str = None
    
    
        display_cursor_badge(co2d, symbol, badge_mode, message_main, message_aux, aux_num, aux_str)
    
        '''
        if badge is True:
            square = [[17, 30], [17, 40], [27, 40], [27, 30]]
            rectangle = [[27, 30], [27, 40], [67, 40], [67, 30]]
            polysymbol = copy.deepcopy(NP020FP.polysymbol)
            ipx = 29
            ipy = 33
            size = badge_size
            for co in square:
                co[0] = round((co[0] * size), 0) - (size * 10) + co2d[0]
                co[1] = round((co[1] * size), 0) - (size * 25) + co2d[1]
            for co in rectangle:
                co[0] = round((co[0] * size), 0) - (size * 10) + co2d[0]
                co[1] = round((co[1] * size), 0) - (size * 25) + co2d[1]
            for co in polysymbol:
                co[0] = round((co[0] * size), 0) - (size * 10) + co2d[0]
                co[1] = round((co[1] * size), 0) - (size * 25) + co2d[1]
    
            ipx = round((ipx * size), 0) - (size * 10) + co2d[0]
            ipy = round((ipy * size), 0) - (size * 25) + co2d[1]
            ipsize = int(6 * size)
            bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
            bgl.glBegin(bgl.GL_TRIANGLE_FAN)
    
            for x, y in square:
                bgl.glVertex2f(x, y)
            bgl.glEnd()
            bgl.glColor4f(0.5, 0.75, 0.0, 1.0)
            bgl.glBegin(bgl.GL_TRIANGLE_FAN)
    
            for x, y in rectangle:
                bgl.glVertex2f(x, y)
            bgl.glEnd()
            bgl.glColor4f(1.0, 1.0, 1.0, 1.0)
            bgl.glBegin(bgl.GL_LINE_STRIP)
    
            for x, y in polysymbol:
                bgl.glVertex2f(x, y)
            bgl.glEnd()
            bgl.glColor4f(1.0, 1.0, 1.0, 1.0)
            blf.position(font_id, ipx, ipy, 0)
            blf.size(font_id, ipsize, 72)
            blf.draw(font_id, 'NAVIGATE')
        '''
        # NUMERICAL DISTANCE:
    
        '''
        bgl.glColor4f(*col_num_shadow)
        if phase > 0:
            font_id = 0
            blf.size(font_id, 20, 72)
            blf.position(font_id, (numloc[0] - 1), (numloc[1] - 1), 0)
            blf.draw(font_id, dist)
    
        bgl.glColor4f(*col_num_main)
        if phase > 0:
            font_id = 0
            blf.size(font_id, 20, 72)
            blf.position(font_id, numloc[0], numloc[1], 0)
            blf.draw(font_id, dist)
        '''
    
        region = bpy.context.region
        rv3d = bpy.context.region_data
        dist_scale = 100
        suffix = ' cm'
        num_size = 20
    
        display_distance_between_two_points(region, rv3d, startloc3d, endloc3d)
    
    
        bgl.glLineWidth(1)
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
        np_print('05_callback_NAV_END')
    
    
    # Defining the operator that will enable navigation if user calls it:
    
    class NPFPRunNavigate(bpy.types.Operator):
        bl_idname = "object.np_fp_run_navigate"
        bl_label = "NP FP Run Navigate"
        bl_options = {'INTERNAL'}
    
        def modal(self, context, event):
            np_print('05_run_NAV_START')
            context.area.tag_redraw()
            selob = NP020FP.selob
            start = NP020FP.start
            end = NP020FP.end
            phase = NP020FP.phase
            polyob = NP020FP.polyob
    
            if event.type == 'MOUSEMOVE':
                np_print('05_run_NAV_mousemove_a')
                self.co2d = ((event.mouse_region_x, event.mouse_region_y))
                np_print('05_run_NAV_mousemove_b')
    
            elif event.type in {'LEFTMOUSE', 'SPACE'} and event.value == 'PRESS':
                np_print('05_run_NAV_left_space_press_START')
                bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
                self.co2d = ((event.mouse_region_x, event.mouse_region_y))
                phase = NP020FP.phase
                region = context.region
                rv3d = context.region_data
                co2d = self.co2d
                view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, co2d)
                pointloc = view3d_utils.region_2d_to_origin_3d(region, rv3d, co2d) + view_vector / 5
                start = NP020FP.start
                end = NP020FP.end
                start.hide = False
                end.hide = False
                np_print('phase=', phase)
                if phase == 0:
                    startloc3d = (0.0, 0.0, 0.0)
                    endloc3d = (0.0, 0.0, 0.0)
                    start.location = pointloc
                    end.location = pointloc
                    NP020FP.flag = 'TRANSLATE'
                if phase == 1:
                    startloc3d = NP020FP.startloc3d
                    endloc3d = pointloc
                    end.location = pointloc
                    NP020FP.flag = 'TRANSLATE'
                if phase == 2:
                    startloc3d = NP020FP.startloc3d
                    endloc3d = pointloc
                    end.location = pointloc
                    NP020FP.flag = 'TRANSLATE'
                if phase == 3:
                    startloc3d = (0.0, 0.0, 0.0)
                    endloc3d = (0.0, 0.0, 0.0)
                    start.location = pointloc
                    end.location = pointloc
                    NP020FP.flag = 'EXTRUDE'
                    """
                if phase == 3.5:
                    startloc3d=(0.0,0.0,0.0)
                    endloc3d=(0.0,0.0,0.0)
                    start.location = pointloc
                    end.location = pointloc
                    NP020FP.flag = 'EXTRUDE'
                    """
                NP020FP.start = start
                NP020FP.end = end
                NP020FP.startloc3d = startloc3d
                NP020FP.endloc3d = endloc3d
    
                np_print('05_run_NAV_left_space_press_FINISHED_flag_TRANSLATE')
                return {'FINISHED'}
    
            elif event.type == 'ESC':
                np_print('05_run_NAV_esc_right_any_START')
                bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
                bpy.ops.object.select_all(action='DESELECT')
                start.hide = False
                end.hide = False
    
                start.select_set(True)
                end.select_set(True)
    
                bpy.ops.object.delete('EXEC_DEFAULT')
                if selob is not polyob:
                    for ob in selob:
    
                        ob.select_set(True)
    
                else:
                    polyob.select = True
                NP020FP.startloc3d = (0.0, 0.0, 0.0)
                NP020FP.endloc3d = (0.0, 0.0, 0.0)
                NP020FP.phase = 0
                NP020FP.start = None
                NP020FP.end = None
                NP020FP.dist = None
                NP020FP.polyob = None
                NP020FP.flag = 'TRANSLATE'
                NP020FP.snap = 'VERTEX'
                bpy.context.tool_settings.use_snap = NP020FP.use_snap
                bpy.context.tool_settings.snap_element = NP020FP.snap_element
                bpy.context.tool_settings.snap_target = NP020FP.snap_target
                bpy.context.space_data.pivot_point = NP020FP.pivot_point
                bpy.context.space_data.transform_orientation = NP020FP.trans_orient
    
                if NP020FP.acob is not None:
    
                    bpy.context.view_layer.objects.active = NP020FP.acob
    
                    bpy.ops.object.mode_set(mode=NP020FP.edit_mode)
                np_print('05_run_NAV_esc_right_any_CANCELLED')
                return{'CANCELLED'}
    
            elif event.type == 'RIGHTMOUSE' and phase > 1:
                bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
                end.location = start.location
                start.hide = True
                end.hide = True
                NP020FP.flag = 'CLOSE'
                np_print('04_run_TRANS_space_FINISHED_flag_NAVIGATE')
                return{'FINISHED'}
    
            elif event.type in {'MIDDLEMOUSE', 'WHEELUPMOUSE', 'WHEELDOWNMOUSE'}:
                np_print('05_run_NAV_middle_wheel_any_PASS_THROUGH')
                return {'PASS_THROUGH'}
            np_print('05_run_NAV_RUNNING_MODAL')
            return {'RUNNING_MODAL'}
    
        def invoke(self, context, event):
            np_print('05_run_NAV_INVOKE_a')
            flag = NP020FP.flag
            phase = NP020FP.phase  # is this used ?
            np_print('flag=', flag)
            self.co2d = ((event.mouse_region_x, event.mouse_region_y))
            if flag == 'NAVIGATE':
                args = (self, context)
                self._handle = bpy.types.SpaceView3D.draw_handler_add(draw_callback_px_NAV, args, 'WINDOW', 'POST_PIXEL')
                context.window_manager.modal_handler_add(self)
                np_print('05_run_NAV_INVOKE_a_RUNNING_MODAL')
                return {'RUNNING_MODAL'}
            else:
                np_print('05_run_NAV_INVOKE_a_FINISHED')
                return {'FINISHED'}
    
    
    # Changing the mode of operating and leaving start point at the first snap, continuing with just the end point:
    
    class NPFPChangePhase(bpy.types.Operator):
        bl_idname = "object.np_fp_change_phase"
        bl_label = "NP FP Change Phase"
        bl_options = {'INTERNAL'}
    
        def execute(self, context):
            np_print('06_change_phase_START')
            NP020FP.phase = 1
            np_print('NP020FP.phase=', NP020FP.phase)
            start = NP020FP.start
            end = NP020FP.end
            startloc3d = start.location
            endloc3d = end.location
            NP020FP.startloc3d = startloc3d
            NP020FP.endloc3d = endloc3d
            bpy.ops.object.select_all(action='DESELECT')
    
            end.select_set(True)
    
            NP020FP.snap = NP020FP.snap
            bpy.context.tool_settings.use_snap = False
            bpy.context.tool_settings.snap_element = NP020FP.snap
            bpy.context.tool_settings.snap_target = 'ACTIVE'
            bpy.context.space_data.pivot_point = 'ACTIVE_ELEMENT'
            bpy.context.space_data.transform_orientation = 'GLOBAL'
            NP020FP.flag = 'TRANSLATE'
            np_print('06_change_phase_END_flag_TRANSLATE')
            return {'FINISHED'}
    
    
    # Changing the mode of operating and leaving start point at the first snap, continuing with just the end point:
    
    class NPFPMakeSegment(bpy.types.Operator):
        bl_idname = "object.np_fp_make_segment"
        bl_label = "NP FP Make Segment"
        bl_options = {'INTERNAL'}
    
        def execute(self, context):
    
            addon_prefs = context.preferences.addons[__package__].preferences
    
            wire = addon_prefs.npfp_wire
            smooth = addon_prefs.npfp_smooth  # is this used ?
            material = addon_prefs.npfp_material
    
            flag = NP020FP.flag
            if flag == 'SURFACE':
                return {'FINISHED'}
            np_print('08_make_segment_START')
            np_print('NP020FP.phase =', NP020FP.phase)
            polyob = NP020FP.polyob
            start = NP020FP.start
            end = NP020FP.end
            startloc3d = start.location
            endloc3d = end.location
            if NP020FP.phase < 2:
                polyverts = []
                polyverts.append(startloc3d)
                polyverts.append(endloc3d)
                polyedges = []
                polyedges.append((1, 0))
                polyfaces = []
                polyme = bpy.data.meshes.new('float_poly')
                polyme.from_pydata(polyverts, polyedges, polyfaces)
                polyob = bpy.data.objects.new('float_poly', polyme)
                polyob.location = mathutils.Vector((0, 0, 0))
                scn = bpy.context.scene
                scn.objects.link(polyob)
                scn.objects.active = polyob
                scn.update()
                bpy.ops.object.select_all(action='DESELECT')
                polyob.select = True
                bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
                if wire:
                    polyob.show_wire = True
                if material:
                    mtl = bpy.data.materials.new('float_poly_material')
                    mtl.diffuse_color = (1.0, 1.0, 1.0)
                    polyme.materials.append(mtl)
                activelayer = bpy.context.scene.active_layer
                np_print('activelayer:', activelayer)
                layers = [False, False, False, False, False, False, False, False, False, False,
                          False, False, False, False, False, False, False, False, False, False]
                layers[activelayer] = True
                layers = tuple(layers)
                np_print(layers)
                bpy.ops.object.move_to_layer(layers=layers)
                bpy.ops.object.select_all(action='DESELECT')
                NP020FP.polyob = polyob
            else:
                polyme = polyob.data
                bm = bmesh.new()
                bm.from_mesh(polyme)
                vco = endloc3d - polyob.location
                bmesh.ops.create_vert(bm, co=vco)
                i = len(bm.verts)
                bm.verts.ensure_lookup_table()
                if flag == 'CLOSE':
                    verts = [bm.verts[i - 1], bm.verts[0]]
                    NP020FP.flag = 'SURFACE'
                else:
                    verts = [bm.verts[i - 1], bm.verts[i - 2]]
                bmesh.ops.contextual_create(bm, geom=verts)
                bm.to_mesh(polyme)
                bm.free()
                bpy.ops.object.select_all(action='DESELECT')
                polyob.select = True
                bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
                NP020FP.polyob = polyob
    
            bpy.context.view_layer.objects.active = polyob
    
            bpy.ops.object.mode_set(mode='EDIT')
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.mesh.remove_doubles()
            bpy.ops.object.mode_set(mode='OBJECT')
    
            bpy.context.view_layer.objects.active = end
    
            start.location = endloc3d
            end.location = endloc3d
            bpy.ops.object.select_all(action='DESELECT')
    
            end.select_set(True)
    
            NP020FP.start = start
            NP020FP.end = end
            NP020FP.phase = 2
            NP020FP.selob = polyob
            NP020FP.acob = polyob
            bpy.context.tool_settings.use_snap = False
            bpy.context.tool_settings.snap_element = NP020FP.snap
            bpy.context.tool_settings.snap_target = 'ACTIVE'
            bpy.context.space_data.pivot_point = 'MEDIAN_POINT'
            bpy.context.space_data.transform_orientation = 'GLOBAL'
            if flag not in {'CLOSE', 'SURFACE'}:
                NP020FP.flag = 'TRANSLATE'
            np_print('08_make_segment_END_flag_TRANSLATE')
            return {'FINISHED'}
    
    
    # Deleting the anchors after succesfull translation and reselecting previously selected objects:
    
    class NPFPDeletePoints(bpy.types.Operator):
        bl_idname = "object.np_fp_delete_points"
        bl_label = "NP FP Delete Points"
        bl_options = {'INTERNAL'}
    
        def execute(self, context):
    
            addon_prefs = context.preferences.addons[__package__].preferences  # is this used ?
    
            dist = NP020FP.dist  # is this used ?
            polyob = NP020FP.polyob
            np_print('07_delete_points_START')
    
            selob = NP020FP.selob
            start = NP020FP.start
            end = NP020FP.end
            bpy.ops.object.select_all(action='DESELECT')
            start.hide = False
            end.hide = False
    
            start.select_set(True)
            end.select_set(True)
    
            bpy.ops.object.delete('EXEC_DEFAULT')
            if selob is not polyob:
                for ob in selob:
    
                    ob.select_set(True)
    
            else:
                polyob.select = True
            NP020FP.startloc3d = (0.0, 0.0, 0.0)
            NP020FP.endloc3d = (0.0, 0.0, 0.0)
            NP020FP.phase = 0
            NP020FP.flag = 'SURFACE'
            NP020FP.start = None
            NP020FP.end = None
            NP020FP.dist = None
            NP020FP.polyverts = []
            NP020FP.polyedges = []
            NP020FP.snap = 'VERTEX'
            bpy.context.tool_settings.use_snap = NP020FP.use_snap
            bpy.context.tool_settings.snap_element = NP020FP.snap_element
            bpy.context.tool_settings.snap_target = NP020FP.snap_target
            bpy.context.space_data.pivot_point = NP020FP.pivot_point
            bpy.context.space_data.transform_orientation = NP020FP.trans_orient
            if NP020FP.acob is not None:
    
                bpy.context.view_layer.objects.active = NP020FP.acob
    
                bpy.ops.object.mode_set(mode=NP020FP.edit_mode)
            return {'FINISHED'}
    
    
    # Creating a surface on the closed polyline:
    
    class NPFPMakeSurface(bpy.types.Operator):
        bl_idname = "object.np_fp_make_surface"
        bl_label = "NP FP Make Surface"
        bl_options = {'INTERNAL'}
    
        def execute(self, context):
    
            addon_prefs = context.preferences.addons[__package__].preferences
    
            smooth = addon_prefs.npfp_smooth
            dist = NP020FP.dist  # is this used ?
            polyob = NP020FP.polyob
            polyme = polyob.data
            bm = bmesh.new()
            bm.from_mesh(polyme)
            bmesh.ops.contextual_create(bm, geom=bm.edges)
            bm.to_mesh(polyme)
            bm.free()
            bpy.ops.object.select_all(action='DESELECT')
            polyob.select = True
            if smooth:
                bpy.ops.object.shade_smooth()
                np_print('smooth ON')
            bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
            bpy.ops.object.select_all(action='DESELECT')
            NP020FP.flag = 'EXTRUDE'
            NP020FP.phase = 3
            return {'FINISHED'}
    
    
    def draw_callback_px_NAVEX(self, context):
    
        np_print('08_callback_NAVEX_START')
    
    
        addon_prefs = context.preferences.addons[__package__].preferences
    
    1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
    
        #scale = addon_prefs.npfp_scale  # is this used ?
        #badge = addon_prefs.npfp_badge
        #point_size = addon_prefs.npfp_point_size  # is this used ?
        #badge_size = addon_prefs.npfp_badge_size
    
        #polyob = NP020FP.polyob  # is this used ?
        #phase = NP020FP.phase  # is this used ?
        #region = context.region  # is this used ?
        #rv3d = context.region_data  # is this used ?
        co2d = self.co2d
    
        if addon_prefs.npfp_col_line_main_DEF is False:
            col_line_main = addon_prefs.npfp_col_line_main  # is this used ?
        else:
            col_line_main = (1.0, 1.0, 1.0, 1.0)  # is this used ?
    
        if addon_prefs.npfp_col_line_shadow_DEF is False:
            col_line_shadow = addon_prefs.npfp_col_line_shadow  # is this used ?
        else:
            col_line_shadow = (0.1, 0.1, 0.1, 0.25)  # is this used ?
    
        if addon_prefs.npfp_col_num_main_DEF is False:
            col_num_main = addon_prefs.npfp_col_num_main  # is this used ?
        else:
            col_num_main = (0.1, 0.1, 0.1, 0.75)  # is this used ?
    
        if addon_prefs.npfp_col_num_shadow_DEF is False:
            col_num_shadow = addon_prefs.npfp_col_num_shadow  # is this used ?
        else:
            col_num_shadow = (1.0, 1.0, 1.0, 1.0)  # is this used ?
    
        if addon_prefs.npfp_point_color_DEF is False:
            col_pointromb = addon_prefs.npfp_point_color  # is this used ?
        else:
            col_pointromb = (0.3, 0.3, 0.3, 1.0)  # is this used ?
    
        '''
        if addon_prefs.npfp_suffix == 'None':
            suffix = None  # is this used ?
    
        elif addon_prefs.npfp_suffix in ('km', 'm', 'cm', 'mm', 'nm', 'thou'):
            suffix = ' '.join(addon_prefs.npfp_suffix)
    
        elif addon_prefs.npfp_suffix == "'":
            suffix = "'"  # is this used ?
    
        elif addon_prefs.npfp_suffix == '"':
            suffix = '"'   # is this used ?
        '''
        # Calculating the 3d points for the graphical line while in NAVIGATE flag:
    
        """
    
        np_print('phase=', phase)
        if phase == 0 or phase == 3:
            startloc3d=(0.0,0.0,0.0)
            endloc3d=(0.0,0.0,0.0)
    
        if phase == 1:
            startloc3d = NP020FP.startloc3d
            endloc3d = pointloc
    
        if phase == 2:
            startloc3d = NP020FP.startloc3d
            endloc3d = pointloc
        """
        # Calculating the 2D points for the graphical line while in NAVIGATE flag from 3D points:
        """
        startloc2d = view3d_utils.location_3d_to_region_2d(region, rv3d, startloc3d)
        endloc2d = view3d_utils.location_3d_to_region_2d(region, rv3d, endloc3d)
    
        if startloc2d == None:
            startloc2d=(0.0,0.0)
            endloc2d=(0.0,0.0)
        np_print(startloc2d, endloc2d)
    
        dist = (Vector(endloc3d) - Vector(startloc3d))
        dist = dist.length*scale
        np_print(dist)
    
        if suffix is not None:
            dist = str(abs(round(dist,2))) + suffix
        else:
            dist = str(abs(round(dist,2)))
    
        NP020FP.dist = dist
        np_print(dist)
    
        #This is for correcting the position of the numerical on the screen if the endpoints are far out of screen:
        numloc = []
        startx=startloc2d[0]
        starty=startloc2d[1]
        endx=endloc2d[0]
        endy=endloc2d[1]
        if startx > region.width:
            startx = region.width
        if startx < 0:
            startx = 0
        if starty > region.height:
            starty = region.height
        if starty < 0:
            starty = 0
        if endx > region.width:
            endx = region.width
        if endx < 0:
            endx = 0
        if endy > region.height:
            endy = region.height
        if endy < 0:
            endy = 0
        numloc.append((startx+endx)/2)
        numloc.append((starty+endy)/2)
    
        if phase==0:
            main='NAVIGATE FOR BETTER PLACEMENT OF START POINT'
    
        if phase==1:
            main='NAVIGATE FOR BETTER PLACEMENT OF NEXT POINT'
    
        if phase==2:
            main='NAVIGATE FOR BETTER PLACEMENT OF NEXT POINT'
    
        if phase==3:
            main='NAVIGATE FOR BETTER PLACEMENT OF EXTRUSION HEIGHT'
    
    
        #Drawing:
    
        bgl.glEnable(bgl.GL_BLEND)
    
        #LINE:
    
        bgl.glColor4f(*col_line_shadow)
        bgl.glLineWidth(1.4)
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex2f((startloc2d[0]-1),(startloc2d[1]-1))
        bgl.glVertex2f((endloc2d[0]-1),(endloc2d[1]-1))
        bgl.glEnd()
    
        bgl.glColor4f(*col_line_main)
        bgl.glLineWidth(1.4)
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex2f(*startloc2d)
        bgl.glVertex2f(*endloc2d)
        bgl.glEnd()
    
        #POINT MARKERS:
        markersize = point_size
        triangle = [[0, 0], [-1, 1], [1, 1]]
        romb = [[-1, 0], [0, 1], [1, 0], [0, -1]]
        if phase > 0 and polyob is None:
            polylist2d = []
            for co in triangle:
                co[0] = round((co[0] * markersize * 3),0) + startloc2d[0]
                co[1] = round((co[1] * markersize * 3),0) + startloc2d[1]
            np_print('triangle', triangle)
            bgl.glColor4f(1.0, 0.5, 0.0, 1.0)
            bgl.glBegin(bgl.GL_TRIANGLE_FAN)
            for x,y in triangle:
                bgl.glVertex2f(x,y)
            bgl.glEnd()
            pointromb = [[-1, 0], [0, 1], [1, 0], [0, -1]]
            for co in pointromb:
                co[0] = round((co[0] * markersize),0) + endloc2d[0]
                co[1] = round((co[1] * markersize),0) + endloc2d[1]
            if phase == 2:
                np_print('pointromb', pointromb)
                bgl.glColor4f(*col_pointromb)
                bgl.glBegin(bgl.GL_TRIANGLE_FAN)
                for x,y in pointromb:
                    bgl.glVertex2f(x,y)
                bgl.glEnd()
        if polyob is not None:
            np_print('polyob not None')
            polyloc = polyob.location
            polyme = polyob.data
            polylist3d = []
            for me in polyme.vertices:
                polylist3d.append(me.co + polyloc)
            np_print('polylist3d = ', polylist3d)
            polylist2d = []
            for p3d in polylist3d:
                p2d = view3d_utils.location_3d_to_region_2d(region, rv3d, p3d)
                polylist2d.append(p2d)
            np_print('polylist2d = ', polylist2d)
            #triangle for the first point
            for co in triangle:
                co[0] = round((co[0] * markersize * 3),0) + polylist2d[0][0]
                co[1] = round((co[1] * markersize * 3),0) + polylist2d[0][1]
            np_print('triangle', triangle)
            bgl.glColor4f(1.0, 0.5, 0.0, 1.0)
            bgl.glBegin(bgl.GL_TRIANGLE_FAN)
            for x,y in triangle:
                bgl.glVertex2f(x,y)
            bgl.glEnd()
            #rombs for the other points
            i = 0
            for p2d in polylist2d:
                if i > 0:
                    pointromb = [[-1, 0], [0, 1], [1, 0], [0, -1]]
                    for co in pointromb:
                        co[0] = round((co[0] * markersize),0) + p2d[0]
                        co[1] = round((co[1] * markersize),0) + p2d[1]
                    np_print('pointromb', pointromb)
                    bgl.glColor4f(*col_pointromb)
                    bgl.glBegin(bgl.GL_TRIANGLE_FAN)
                    for x,y in pointromb:
                        bgl.glVertex2f(x,y)
                i = i + 1
                bgl.glEnd()
    
        #NUMERICAL DISTANCE:
    
        bgl.glColor4f(*col_num_shadow)
        if phase > 0:
            font_id = 0
            blf.size(font_id, 20, 72)
            blf.position(font_id, (numloc[0]-1), (numloc[1]-1), 0)
            blf.draw(font_id, dist)
    
        bgl.glColor4f(*col_num_main)
        if phase > 0:
            font_id = 0
            blf.size(font_id, 20, 72)
            blf.position(font_id, numloc[0], numloc[1], 0)
            blf.draw(font_id, dist)
    
        bgl.glLineWidth(1)
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
        """
        # ON-SCREEN INSTRUCTIONS:
    
        main = 'navigate for better placement of extrusion height'
    
        # ON-SCREEN INSTRUCTIONS:
    
        region = bpy.context.region
        rv3d = bpy.context.region_data
        instruct = main
        keys_aff = 'MMB, SCROLL - navigate'
        keys_nav = 'LMB, SPACE - leave navigate'
        keys_neg = 'ESC, RMB - quit'
    
        display_instructions(region, rv3d, instruct, keys_aff, keys_nav, keys_neg)
    
    
        font_id = 0
    
        # Drawing the small badge near the cursor with the basic instructions:
    
        if badge is True:
            square = [[17, 30], [17, 40], [27, 40], [27, 30]]
            rectangle = [[27, 30], [27, 40], [67, 40], [67, 30]]
            polysymbol = copy.deepcopy(NP020FP.polysymbol)
            ipx = 29
            ipy = 33
            size = badge_size
            for co in square:
                co[0] = round((co[0] * size), 0) - (size * 10) + co2d[0]
                co[1] = round((co[1] * size), 0) - (size * 25) + co2d[1]
            for co in rectangle:
                co[0] = round((co[0] * size), 0) - (size * 10) + co2d[0]
                co[1] = round((co[1] * size), 0) - (size * 25) + co2d[1]
            for co in polysymbol:
                co[0] = round((co[0] * size), 0) - (size * 10) + co2d[0]
                co[1] = round((co[1] * size), 0) - (size * 25) + co2d[1]
    
            ipx = round((ipx * size), 0) - (size * 10) + co2d[0]
            ipy = round((ipy * size), 0) - (size * 25) + co2d[1]
            ipsize = int(6 * size)
            bgl.glColor4f(0.0, 0.0, 0.0, 0.0)
            bgl.glBegin(bgl.GL_TRIANGLE_FAN)
    
            for x, y in square:
                bgl.glVertex2f(x, y)
            bgl.glEnd()
            bgl.glColor4f(0.5, 0.75, 0.0, 1.0)
            bgl.glBegin(bgl.GL_TRIANGLE_FAN)
    
            for x, y in rectangle:
                bgl.glVertex2f(x, y)
    
            bgl.glEnd()
            bgl.glColor4f(1.0, 1.0, 1.0, 1.0)
            bgl.glBegin(bgl.GL_LINE_STRIP)
    
            for x, y in polysymbol:
                bgl.glVertex2f(x, y)
            bgl.glEnd()
            bgl.glColor4f(1.0, 1.0, 1.0, 1.0)
            blf.position(font_id, ipx, ipy, 0)
            blf.size(font_id, ipsize, 72)
            blf.draw(font_id, 'NAVIGATE')
    
        bgl.glLineWidth(1)
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
        np_print('05_callback_NAVEX_END')
    
    
    # Defining the operator that will draw the OpenGL the numerical distance and
    # the on-screen instructions while the user extrudes the surface:
    
    class NPFPRunNavEx(bpy.types.Operator):
        bl_idname = "object.np_fp_run_navex"
        bl_label = "NP FP Run NavEx"
        bl_options = {'INTERNAL'}
    
        def modal(self, context, event):
            np_print('05_run_NAVEX_START')
            context.area.tag_redraw()
            selob = NP020FP.selob
            phase = NP020FP.phase
    
            if event.type == 'MOUSEMOVE':
                np_print('05_run_NAVEX_mousemove_a')
                self.co2d = ((event.mouse_region_x, event.mouse_region_y))
                np_print('05_run_NAVEX_mousemove_b')
    
            elif event.type in {'LEFTMOUSE', 'SPACE'} and event.value == 'PRESS':
                np_print('05_run_NAVEX_left_space_press_START')
                bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
                self.co2d = ((event.mouse_region_x, event.mouse_region_y))
                region = context.region
                rv3d = context.region_data
                co2d = self.co2d
                view_vector = view3d_utils.region_2d_to_vector_3d(region, rv3d, co2d)
                pointloc = view3d_utils.region_2d_to_origin_3d(region, rv3d, co2d) + view_vector / 5  # is this used ?
                np_print('phase=', phase)
                NP020FP.flag = 'EXTRUDE'
                np_print('05_run_NAVEX_left_space_press_FINISHED_flag_TRANSLATE')
                return {'FINISHED'}
    
            elif event.type in {'RIGHTMOUSE', 'ESC'}:
                np_print('05_run_NAVEX_esc_right_any_START')
                bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
                bpy.ops.object.mode_set(mode='OBJECT')
                bpy.ops.object.select_all(action='DESELECT')
    
                # XXX selob and polyob are undefined
                if selob is not polyob:
                    for ob in selob:
    
                        ob.select_set(True)
    
                else:
                    polyob.select = True
    
                NP020FP.startloc3d = (0.0, 0.0, 0.0)
                NP020FP.endloc3d = (0.0, 0.0, 0.0)
                NP020FP.phase = 0
                NP020FP.start = None
                NP020FP.end = None
                NP020FP.dist = None
                NP020FP.polyob = None
                NP020FP.flag = 'TRANSLATE'
                NP020FP.snap = 'VERTEX'
                bpy.context.tool_settings.use_snap = NP020FP.use_snap
                bpy.context.tool_settings.snap_element = NP020FP.snap_element
                bpy.context.tool_settings.snap_target = NP020FP.snap_target
                bpy.context.space_data.pivot_point = NP020FP.pivot_point
                bpy.context.space_data.transform_orientation = NP020FP.trans_orient
                if NP020FP.acob is not None:
    
                    bpy.context.view_layer.objects.active = NP020FP.acob
    
                    bpy.ops.object.mode_set(mode=NP020FP.edit_mode)
                np_print('05_run_NAVEX_esc_right_any_CANCELLED')
                return{'CANCELLED'}
    
            elif event.type in {'MIDDLEMOUSE', 'WHEELUPMOUSE', 'WHEELDOWNMOUSE'}:
                np_print('05_run_NAVEX_middle_any_PASS_THROUGH')
                return {'PASS_THROUGH'}
    
            np_print('05_run_NAVEX_RUNNING_MODAL')
            return {'RUNNING_MODAL'}
    
        def invoke(self, context, event):
            np_print('05_run_NAVEX_a')
            flag = NP020FP.flag
            np_print('flag=', flag)
            self.co2d = ((event.mouse_region_x, event.mouse_region_y))
            if flag == 'NAVEX':
                args = (self, context)
                self._handle = bpy.types.SpaceView3D.draw_handler_add(draw_callback_px_NAVEX, args, 'WINDOW', 'POST_PIXEL')
                context.window_manager.modal_handler_add(self)
                return {'RUNNING_MODAL'}
            else:
                np_print('05_run_NAVEX_INVOKE_a_FINISHED')
                return {'FINISHED'}
    
    
    def draw_callback_px_EXTRUDE(self, context):
    
        np_print('10_callback_EXTRUDE_START')
    
    
        addon_prefs = context.preferences.addons[__package__].preferences
    
        #scale = addon_prefs.npfp_scale
        #badge = addon_prefs.npfp_badge  # is this used ?
        #point_size = addon_prefs.npfp_point_size  # is this used ?
        #badge_size = addon_prefs.npfp_badge_size  # is this used ?
    
        '''
        if addon_prefs.npfp_col_line_main_DEF is False:
            col_line_main = addon_prefs.npfp_col_line_main  # is this used ?
        else:
            col_line_main = (1.0, 1.0, 1.0, 1.0)  # is this used ?
    
        if addon_prefs.npfp_col_line_shadow_DEF is False:
            col_line_shadow = addon_prefs.npfp_col_line_shadow  # is this used ?
        else:
            col_line_shadow = (0.1, 0.1, 0.1, 0.25)  # is this used ?
    
        if addon_prefs.npfp_col_num_main_DEF is False:
            col_num_main = addon_prefs.npfp_col_num_main  # is this used ?
        else:
            col_num_main = (0.1, 0.1, 0.1, 0.75)  # is this used ?
    
        if addon_prefs.npfp_col_num_shadow_DEF is False:
            col_num_shadow = addon_prefs.npfp_col_num_shadow  # is this used ?
        else:
            col_num_shadow = (1.0, 1.0, 1.0, 1.0)  # is this used ?
    
        if addon_prefs.npfp_point_color_DEF is False:
            col_pointromb = addon_prefs.npfp_point_color  # is this used ?
        else:
            col_pointromb = (0.3, 0.3, 0.3, 1.0)  # is this used ?
    
    
        if addon_prefs.npfp_suffix == 'None':
            suffix = None
    
        elif addon_prefs.npfp_suffix in ('km', 'm', 'cm', 'mm', 'nm', 'thou'):
            suffix = ' '.join(addon_prefs.npfp_suffix)
    
        elif addon_prefs.npfp_suffix == "'":
            suffix = "'"
    
        elif addon_prefs.npfp_suffix == '"':
            suffix = '"'
        '''
        # np_print('0')
        # sel = bpy.context.selected_objects
        flag = NP020FP.flag
        polyob = NP020FP.polyob
        polyme = polyob.data
        i = len(polyme.vertices)
        np_print('extrude_callback')
        np_print('i', i)
        j = i / 2
        j = int(j)
        np_print(i, j)
        np_print(polyme.vertices[0].co)
        startloc3d = polyme.vertices[0].co
        endloc3d = polyme.vertices[j].co
        np_print('Ss3d, Se3d', startloc3d, endloc3d)
        region = context.region
        rv3d = context.region_data
    
    
    
        '''
        startloc2d = view3d_utils.location_3d_to_region_2d(region, rv3d, startloc3d)
        endloc2d = view3d_utils.location_3d_to_region_2d(region, rv3d, endloc3d)
        np_print(startloc2d, endloc2d)
        # np_print('1')
    
        dist = (mathutils.Vector(endloc3d) - mathutils.Vector(startloc3d))
        dist = dist.length * scale
        np_print('dist =', dist)
        if suffix is not None:
            dist = str(abs(round(dist, 2))) + suffix
        else:
            dist = str(abs(round(dist, 2)))
        np_print('dist =', dist)
        NP020FP.dist = dist
        # np_print('2')
        # This is for correcting the position of the numerical on the screen if the endpoints are far out of screen:
        numloc = []
        startx = startloc2d[0]
        starty = startloc2d[1]
        endx = endloc2d[0]
        endy = endloc2d[1]
        if startx > region.width:
            startx = region.width
        if startx < 0:
            startx = 0
        if starty > region.height:
            starty = region.height
        if starty < 0:
            starty = 0
        if endx > region.width:
            endx = region.width
        if endx < 0:
            endx = 0
        if endy > region.height:
            endy = region.height
        if endy < 0:
            endy = 0
        numloc.append((startx + endx) / 2)
        numloc.append((starty + endy) / 2)
        # np_print('3')
        '''
    
        # Drawing:
        """
        bgl.glEnable(bgl.GL_BLEND)
    
        #LINE:
    
        bgl.glColor4f(*col_line_shadow)
        bgl.glLineWidth(1.4)
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex2f((startloc2d[0]-1),(startloc2d[1]-1))
        bgl.glVertex2f((endloc2d[0]-1),(endloc2d[1]-1))
        bgl.glEnd()
    
        bgl.glColor4f(*col_line_main)
        bgl.glLineWidth(1.4)
        bgl.glBegin(bgl.GL_LINE_STRIP)
        bgl.glVertex2f(*startloc2d)
        bgl.glVertex2f(*endloc2d)
        bgl.glEnd()
        #np_print('4')
        #NUMERICAL DISTANCE:
    
        np_print('numloc = ' ,numloc, 'dist = ', dist)
    
        bgl.glColor4f(*col_num_shadow)
        font_id = 0
        blf.size(font_id, 20, 72)
        blf.position(font_id, (numloc[0]-1), (numloc[1]-1), 0)
        blf.draw(font_id, dist)
    
        bgl.glColor4f(*col_num_main)
        font_id = 0
        blf.size(font_id, 20, 72)
        blf.position(font_id, numloc[0], numloc[1], 0)
        blf.draw(font_id, dist)
    
        bgl.glLineWidth(1)
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
    
        """
        # np_print('5')