CamBam
News:
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 16, 2018, 13:29:36 pm


Login with username, password and session length


Pages: [1] 2
  Print  
Author Topic: Vb to Python  (Read 1618 times)
Dragonfly
CNC Jedi
*****
Offline Offline

Posts: 2036



View Profile
« on: July 27, 2018, 17:33:13 pm »

Hi all,
here is a snippet from the code snippets section.
How can it be converted to a Python script if possible? VB script is not supported in Linux version of CB.
Code:
' New CamBam VBScript

sub main

'get info about a plugin (ex with the NumMove plugin)

    Dim fvi As System.Diagnostics.FileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(My.Application.Info.DirectoryPath + "\plugins\NumMove.dll")

    CamBam.ThisApplication.AddLogMessage(fvi.FileName)
    CamBam.ThisApplication.AddLogMessage(fvi.CompanyName)
    CamBam.ThisApplication.AddLogMessage(fvi.LegalCopyright)
    CamBam.ThisApplication.AddLogMessage(fvi.Comments)
    CamBam.ThisApplication.AddLogMessage(fvi.ProductName)

    CamBam.ThisApplication.AddLogMessage(fvi.ProductVersion)

end sub
Logged

Before asking a question do some effort and walk through all menus and options in CamBam.  Maybe the answer is there. Please.
onekk
Wookie
****
Offline Offline

Posts: 394


View Profile
« Reply #1 on: July 27, 2018, 17:56:26 pm »

Sadly Python scripting is not very well documented, I've tried to use it but I've never succeeded in doing something, some example are working.

As a side note the python "interpreter " is IronPython.dll (a quick look at the main CamBam program directory reveal that), so there is no way to "spy" the proper class with a python editor.

I'm too interested in the possibility of using python as a scripting language, but the API is not "linear" maybe now that Andy is listening at the forum maybe it will make some light on Python scripting.

Regards

Carlo D.

   
Logged

Carlo D. (onekk)

eShapeoko #343 750x1000 mm + GRBL + bCNC + CamBam
dh42
Administrator
CNC Jedi
*****
Offline Offline

Posts: 5214



View Profile WWW
« Reply #2 on: July 27, 2018, 17:58:45 pm »

Hello

Quote
How can it be converted to a Python script if possible?

I'm very interested too ; I hate python !!! ... but it's the only language that works with the new Script object of CB 1.0.

++
David
Logged
dave benson
CNC Jedi
*****
Offline Offline

Posts: 1010


View Profile
« Reply #3 on: August 03, 2018, 15:24:19 pm »

Hi Guy's
Yes I'm not a fan of scripting languages either, but if you want to do some scripting in python, you could look at Pycharm which may help get you started.

Dave
Logged
Dragonfly
CNC Jedi
*****
Offline Offline

Posts: 2036



View Profile
« Reply #4 on: August 03, 2018, 17:07:28 pm »

Dave,
my question was not about writing in Python in general.
But rather how to link to CamBam API and repeat what the example code snippet does in VB.
Logged

Before asking a question do some effort and walk through all menus and options in CamBam.  Maybe the answer is there. Please.
dave benson
CNC Jedi
*****
Offline Offline

Posts: 1010


View Profile
« Reply #5 on: August 04, 2018, 05:51:13 am »

Hi Fly
 Do this:
Code:
from System import Math
from System import Array
from System.Collections.Generic import List
from CamBam.UI import CamBamUI
from CamBam.Geom import *
from CamBam.CAD import *
Dave
Logged
dave benson
CNC Jedi
*****
Offline Offline

Posts: 1010


View Profile
« Reply #6 on: August 04, 2018, 07:43:12 am »

I did a bit of fiddling about and got a result.
I think that to do this successfully in even in CB you need to know something about python in general.
Dave


* reply for Dragonfly python ok.PNG (150.95 KB, 1135x718 - viewed 34 times.)
Logged
EddyCurrent
CNC Jedi
*****
Offline Offline

Posts: 3659



View Profile
« Reply #7 on: August 05, 2018, 11:26:58 am »

I don't use Linux but it looks like Powershell would be a good install; https://www.howtogeek.com/267858/how-to-install-microsoft-powershell-on-linux-or-os-x/
Logged
Dragonfly
CNC Jedi
*****
Offline Offline

Posts: 2036



View Profile
« Reply #8 on: August 05, 2018, 19:46:25 pm »

Have to read and understand what Powershell does, never heard about it before.

Dave
I have started writing a Python script thanks to the clues you gave me.
I can check whether a single ply is selected, then select all objects and iterate through the selection.
But when I try to get each object's extremas something is not working as expected. The passed Point3F variables to GetExtrema() are not altered.
Code:
p3max = Point3F(1,1,1)
p3min = Point3F(2,2,2)
p2 = Point2F()
if (view.SelectedEntities.Length == 1):
    for ent in view.Selection:
      if (ent.GetType().Name == "Polyline"):
        poly = Polyline(ent)
        app.log("Polyline:    "+ent.ID.ToString())
        ent.GetExtrema(p3min, p3max)
        s=p3min.ToString()
        app.log(s)  <---- prints 2,2,2 instead of the real values
else:
    app.log("Error: Please select a single poly.")  
This is simplified code. I suppose I am not calling ent.GetExtrema correctly or the two Point3F variables should be passed in a different way. The method requires that variables are passed by reference but AFAIK Python always works this way.
« Last Edit: August 05, 2018, 19:49:20 pm by Dragonfly » Logged

Before asking a question do some effort and walk through all menus and options in CamBam.  Maybe the answer is there. Please.
dh42
Administrator
CNC Jedi
*****
Offline Offline

Posts: 5214



View Profile WWW
« Reply #9 on: August 05, 2018, 20:25:08 pm »

Hello

Strange ; the same code in VB is working and I get the right values for extremas

Code:
' New CamBam VBScript

sub main
    dim p3max as point3f = new Point3F(1,1,1)
    dim p3min as point3f = new Point3F(2,2,2)
    dim poly as Polyline

    if view.SelectedEntities.Length = 1
        For Each ent as Entity In view.SelectedEntities
          if TypeOf ent Is Polyline
            poly = ent
            app.log("Polyline:    " & ent.ID.ToString())
            ent.GetExtrema(p3min, p3max)
            app.log(p3min.ToString())
          end if
        Next
    else
        app.log("Error: Please select a single poly.") 
       
    End if   
end sub

On the python code, I tried a ent.Update() before the GetExtrema, and also the "long" GetExtrema() syntax, but no change.  Huh

++
David
Logged
Dragonfly
CNC Jedi
*****
Offline Offline

Posts: 2036



View Profile
« Reply #10 on: August 06, 2018, 08:21:02 am »

I think the variables are not passed correctly or not initialized correctly due to way they are declared. The reason I assign the value of the .ToString method to a string 's' is because direct usage of the method on the variables return null value error. The script, as it is now, does not work under Linux and windows either.
Anyway, I'll try to make the script I want in VB and then dig into Python again.
When using 'Silhouette' function on a 3D object it produces a stacked set of shapes. The aim is to make a script which reads the Z level of the selected poly and then selects all polylines with the same Z level.
« Last Edit: August 06, 2018, 08:22:43 am by Dragonfly » Logged

Before asking a question do some effort and walk through all menus and options in CamBam.  Maybe the answer is there. Please.
dh42
Administrator
CNC Jedi
*****
Offline Offline

Posts: 5214



View Profile WWW
« Reply #11 on: August 06, 2018, 19:30:53 pm »

Hello

Because the silhouete return always 'flat' polylines, you can use the Polyline.FirstPoint() to get the Z pos of the first point of the line.

seems to works in Python

Code:
if (view.SelectedEntities.Length == 1):
    for ent in view.Selection:
      if (ent.GetType().Name == "Polyline"):
        poly = Polyline(ent)
        app.log("Polyline:    "+ent.ID.ToString())
        p = poly.FirstPoint.Z
        app.log(String(p))  
else:
    app.log("Error: Please select a single poly.")  

++
David
Logged
dave benson
CNC Jedi
*****
Offline Offline

Posts: 1010


View Profile
« Reply #12 on: August 07, 2018, 08:28:57 am »

HI Guys
Well that took some noodling but here you go!
Dave

forgot to add:
The polyline has Z values at 1 and 10 and I was to lazy to modify the polyline names in the extreama function
really they are just points, name them as you wish.


* reply for dragonfly finally got it hooray.PNG (164.08 KB, 1005x710 - viewed 33 times.)
« Last Edit: August 07, 2018, 08:36:44 am by dave benson » Logged
EddyCurrent
CNC Jedi
*****
Offline Offline

Posts: 3659



View Profile
« Reply #13 on: August 08, 2018, 15:13:20 pm »

Dave, I think you may have cheated by just getting the line end points  Cheesy

I believe this is the answer  Grin

Code:
# New CamBam Python Script

polyMin = Point3F()
polyMax = Point3F()

if (view.SelectedEntities.Length == 1):
   for ent in view.SelectedEntities:
     if (ent.GetType() == Polyline):
        poly = Polyline(ent)
        app.log("Polyline: " + ent.ID.ToString())
    
        (polyMin, polyMax) = poly.GetExtrema(polyMin,polyMax)
    
        # get x,y,z values
        app.log("Extrema Min (x,y,z): = " + polyMin.ToString())
        app.log("Extrema Max (x.y.z): = " + polyMax.ToString())

        # example to get Max X value only
        app.log("Extrema Max X : = " + polyMax.X.ToString())
 
else:        
        app.log("Errot: Please select a single poly.")


Apparently this method;

 (polyMin, polyMax) = poly.GetExtrema(polyMin,polyMax)

is called, 'automatic marshalling'

I used the same variables but it could also be like this with two new variables;

Code:

refValue1 = Point3F()
refValue2 = Point3F()

(refValue1, refValue2) = poly.GetExtrema(polyMin,polyMax)


This is a better method to get the entity Type;
 if (ent.GetType() == Polyline):

by using this method;
if (ent.GetType().Name == "Polyline"):

I think it will have problems if CamBam is not set to use English.
« Last Edit: August 08, 2018, 16:27:09 pm by EddyCurrent » Logged
Dragonfly
CNC Jedi
*****
Offline Offline

Posts: 2036



View Profile
« Reply #14 on: August 08, 2018, 16:24:08 pm »

My experience with Python is small, if not tiny.
But this way of assigning values to variables that are passed to the method anyway (and the method is expected to set the corresponding values into them) seems quite strange.

And yes, Dave's example is using an odd case of a simple poly.
Logged

Before asking a question do some effort and walk through all menus and options in CamBam.  Maybe the answer is there. Please.
Pages: [1] 2
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM
Page created in 0.152 seconds with 20 queries.

Copyright © 2018 HexRay Ltd. | Sitemap