Draw Half_circle


#1

Hi
the attached Python_Script draw a half _circle and i share it.


# This Python script draws a half circle dynamically
# Autor  ing. Carlotto Vittorio  carlottovittorio@gmail.com
# 10-10-2013   Bassano del Grappa  Italy
#-*- encoding: utf-8 -*-
import Rhino
import System.Drawing.Color
import scriptcontext
import rhinoscriptsyntax as rs
import math
def half_circle():    
    line_color = System.Drawing.Color.FromArgb(255,255,255)    
    #cur_lay=rs.CurrentLayer()
    #arc_color=rs.LayerColor(cur_lay) 
    position=rs.GetDocumentData("HalfCircle","position")    
    if position=="":position="Right"
    options=["Right","Left"]
    position=rs.GetString("Position of Arc <Enter to confirm>",position,options)
    rs.SetDocumentData("HalfCircle","position",str(position))    
    stringa=position
    if stringa=="Right":
        rad=-math.pi/2
        angle=-90
    if stringa=="Left":
        rad=math.pi/2
        angle=90
    
    while True: 
         
        pt_start=rs.GetPoint("StartPoint of half Circle < Enter for End>")  # con metodo script 
        cur_lay=rs.CurrentLayer()
        arc_color=rs.LayerColor(cur_lay)
        if pt_start==None :break
        arrplane=rs.ViewCPlane()
        ZA=arrplane.ZAxis     
        def GetPointDynamicDrawFunc( sender, args ): 
            #print Rhino.DocObjects.Layer.Color()
            mid_point=(pt_start+args.CurrentPoint)/2            
            args.Display.DrawLine(pt_start, args.CurrentPoint,line_color,1)
            vect_l=pt_start-mid_point            
            Rhino.Geometry.Vector3d.Rotate(vect_l,rad,ZA)                    
            p3=Rhino.Geometry.Point3d.Add(mid_point,vect_l)            
            arc=Rhino.Geometry.Arc(pt_start, p3,args.CurrentPoint)
            args.Display.DrawArc(arc,arc_color,2)            
        gp = Rhino.Input.Custom.GetPoint()        
        gp.DynamicDraw += GetPointDynamicDrawFunc  # chiamo in continuo la funzione che mostra le curve dinamiche
        gp.Get()     
        
        if( gp.CommandResult() == Rhino.Commands.Result.Success ):
            rs.EnableRedraw(False)
            end_point= gp.Point()  # qui ho le coordinate del punto del mouse
            mid_point=(pt_start+end_point)/2                  
            mat_rot=rs.XformRotation2(angle,ZA,mid_point)
            p3=rs.PointTransform(pt_start,mat_rot)                
            arco=rs.AddArc3Pt(pt_start,end_point,p3) 
            rs.EnableRedraw(True)
        
half_circle()

Ciao Vittorio
Disegna_Mezzo_Cerchio_Dinamico_reverse.py(2.4 KB)


#2

Nice Vittorio! I really need to dive into this dynamic redraw stuff…
Ciao, --Mitch


#3

Hi Mitch
I wish could enter the distance of the line in the loop but do not know how to do.
Can you or @steve help me?
Ciao Vittorio


#4

Hey Vittorio,
I really don’t know much about this stuff… But I stole a couple lines out of one of the examples and if what you are looking to do is to enter a diameter after the first point, the attached should do it…

        #I added these three lines shamelessly stolen from Steve's Get Line example
        #User can enter a number to constrain the diameter
        gp.SetBasePoint(pt_start, False)
        gp.SetCommandPrompt("Pick point for end of semicircle")
        gp.DrawLineFromPoint(pt_start, True)

–Mitch

DynamicDrawSemicircle.py(2.7 KB)


(Steve Baer) #5

Here’s my tweaked version with command line options for fixed lengths and side

# This Python script draws a half circle dynamically
# Autor  ing. Carlotto Vittorio  carlottovittorio@gmail.com
# 10-10-2013   Bassano del Grappa  Italy
#-*- encoding: utf-8 -*-
import Rhino
import System.Drawing.Color
import rhinoscriptsyntax as rs
import math
import scriptcontext

def compute_arc(start, end, length, side, zaxis):
    current_point = end
    if length>0:
        direction = end - start
        line = Rhino.Geometry.Line(start, direction, length)
        current_point = line.To
    mid_point = (start+current_point)/2
    vect_l = start - mid_point
    rad = side * math.pi/2
    angle = side * 90
    Rhino.Geometry.Vector3d.Rotate(vect_l,rad,zaxis)
    p3=Rhino.Geometry.Point3d.Add(mid_point,vect_l)
    return Rhino.Geometry.Arc(start, p3, current_point)

def half_circle():
    line_color = System.Drawing.Color.Black
    side = 1
    if scriptcontext.sticky.has_key('side'):
        side = scriptcontext.sticky['side']

    while True:
        pt_start=rs.GetPoint("Start point")
        if not pt_start: return

        current_layer=rs.CurrentLayer()
        arc_color=rs.LayerColor(current_layer)
        ZA = rs.ViewCPlane().ZAxis

        length_option = Rhino.Input.Custom.OptionDouble(0, True, 0)
        side_option = Rhino.Input.Custom.OptionToggle(side==1, "Right", "Left")
        def GetPointDynamicDrawFunc( sender, args ): 
            current_point = args.CurrentPoint
            length = length_option.CurrentValue
            if length>0:
                direction = current_point-pt_start
                line = Rhino.Geometry.Line(pt_start, direction, length)
                current_point = line.To
                args.Display.DrawDottedLine(current_point, args.CurrentPoint, line_color)
            args.Display.DrawLine(pt_start, current_point, line_color, 1)
            
            arc = compute_arc(pt_start, args.CurrentPoint, length, side, ZA)
            args.Display.DrawArc(arc,arc_color,2)

        gp = Rhino.Input.Custom.GetPoint()
        gp.AddOptionDouble("FixedLength",length_option)
        gp.AddOptionToggle("Side", side_option)
        gp.DynamicDraw += GetPointDynamicDrawFunc  # chiamo in continuo la funzione che mostra le curve dinamiche
        while gp.Get()==Rhino.Input.GetResult.Option:
            if side_option.CurrentValue: side = 1
            else: side = -1
            scriptcontext.sticky['side'] = side

        if( gp.CommandResult() == Rhino.Commands.Result.Success ):
            end_point= gp.Point()  # qui ho le coordinate del punto del mouse
            length = length_option.CurrentValue
            arc = compute_arc(pt_start, end_point, length, side, ZA)
            scriptcontext.doc.Objects.AddArc(arc)
            scriptcontext.doc.Views.Redraw()

half_circle()

#6

Hi Steve and Mitch
Many thanks for your help.
Ciao Vittorio


#7

@stevebaer
Ciao Steve
The script half_circle is Ok but d’ont pay attention to ortho(F8) and direction constraint (Tab key) .
That function should i to use?
Ciao Vittorio


(Steve Baer) #8

You just need to set the base point so the GetPoint knows about it.

gp = Rhino.Input.Custom.GetPoint()
gp.SetBasePoint(pt_start, False)
...

#9

It’s a solution , thanks
Ciao Vittorio