Drawing with Python

6dcf6f4f776cdeb04ab600e1675ee97
I have a point and a vector, how do I get this picture

Have a look at this post for a brief general introduction:

In this case, one can implement the DrawLineArrow method:


231024_DrawVectors_GHPython_00.gh (6.4 KB)

1 Like

Thank you. I have a question about how to release point resources

If you mean how to also draw the points, you can do this:


231024_DrawVectors_GHPython_01.gh (7.6 KB)

2 Likes

That’s it. I first used the point, and the graph appeared. I closed the point, but the graph still appears in the

This is the program I wrote

If you need help with debugging, please upload the files in question and screenshots/video captures of the errors you’re getting. See:

I sent it, but it seems to have been delayed due to network issues

from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import Rhino
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
from Grasshopper.Kernel import GH_RuntimeMessageLevel as RML

class MyComponent(component):

def __init__(self):
    self.Size = 1
    
def DrawViewportWires(self,arg):
    arg.Display.DrawArrow(self.line,System.Drawing.Color.Green)
    
    
def RunScript(self,PointA,Vector, Size, Actual):
    ActualVector = 0
    UnitVector = 0
    UnitRVector = 0
    key = True
    
    if Size is None:
        Size = self.Size
    
    if not PointA:
        self.AddRuntimeMessage(RML.Warning, "no PointA")
        key = False
    if not Vector:
        self.AddRuntimeMessage(RML.Warning, "no Vector")
        key = False
    if key:
        
        ActualVector = rg.Vector3d.Multiply(Size, Vector)
        
        self.line =  rg.Line(PointA, ActualVector)
        
        Vector.Unitize()
        UnitVector = rg.Vector3d.Multiply(Size, Vector)
        Vector.Reverse()
        UnitRVector = rg.Vector3d.Multiply(Size, Vector)
        a = self.line

    # return outputs if you have them; here I try it for you:
    return (ActualVector, UnitVector, UnitRVector)

Source program, unable to resolve PointA. Arrows still exist after canceling input

I don’t know what that means. Maybe you can make a video capture that demonstrates the problem. A guess might be that you are not checking if your self.line exists prior to trying to draw it within DrawViewportWires. See line 20 in my last script above for reference.

If you have firewall issues, perhaps consider uploading via a different site/service (e.g. Dropbox, WeTransfer).

The image I sent above demonstrates the problem of the arrow missing from the pointA input, and my program has also been sent out

RunScript is executed multiple times, but your component is only initialised once. So any field like self.line will stay in memory. You need to make sure to empty it on each Run, otherwise it will just draw the last working arrow.
Other than that, throw an exception if your Runscript arguments are None or wrong. Do not transport the error down the pipe by passing default values.

I don’t see any attached Grasshopper or Rhino files, just some mangled code. And I feel like you’re not reading what I’m writing. If you check that the geometry exists prior to drawing it, things should work as expected:

Good luck…

How can I empty it

I know your method is fine, but now it’s my program that has a problem, and I don’t know how to solve it

you could write self.line = None at the top of RunScript(…). And then add a None check in the DrawViewportWires method.

But in any case, it is important to understand how classes work and to have solid basic understanding of programming. The language doesn’t matter here, its all the same in Python/C#/C++…
Print log message and/or attach with a debugger to truly understand how things work in which order. I’m a fan of not presenting you example code, because what helps you is to understand the problem. But this requires that we speak the same language. What are fields etc.