CamBam
News: *** FORUM BACK *** please see 'Latest News' for updates
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2018, 00:11:51 am


Login with username, password and session length


Pages: 1 ... 4 5 [6]
  Print  
Author Topic: [VBS - C# - VB .net] parts of code (snippet)  (Read 36546 times)
EddyCurrent
CNC Jedi
*****
Offline Offline

Posts: 3434



View Profile
« Reply #75 on: January 09, 2017, 18:33:10 pm »

Normally I would go for a local file such as an INI file but that's because I was a DOS user many years ago. I've decided lately that life's too short to worry about such details and I'm going with the easiest option for myself.
I'm thinking that if we are using mono for the Windows Forms emulation then we might as well let it handle the Registry too.
« Last Edit: January 09, 2017, 18:38:11 pm by EddyCurrent » Logged
dh42
Administrator
CNC Jedi
*****
Offline Offline

Posts: 4966



View Profile WWW
« Reply #76 on: January 09, 2017, 19:13:17 pm »

Hello Eddy

Good trick, thanks for that ! ... I thought that Linux was not able to handle windows registry.

On my last Hatch plugin ; I use that in InitPlugin() to detect if linux or win is used.

Code:
Public Shared WinOS As Boolean = True

    Public Shared Sub InitPlugin(ByRef ui As CamBamUI)
        ...

        'test for Linux/Win version

        If My.Computer.Info.OSPlatform = "Unix" Then    'plugin used on linux version
            WinOS = False
        End If

        ThisApplication.AddLogMessage(2, "Hatch plugin Operating System: " & My.Computer.Info.OSPlatform)

    End Sub

++
David
Logged
lloydsp
CNC Jedi
*****
Offline Offline

Posts: 7514



View Profile
« Reply #77 on: January 09, 2017, 19:30:56 pm »

"I thought that Linux was not able to handle windows registry."
-----------
Linux doesn't.  Mono does.

Lloyd
Logged

"Pyro for Fun and Profit for More Than Fifty Years"
dh42
Administrator
CNC Jedi
*****
Offline Offline

Posts: 4966



View Profile WWW
« Reply #78 on: April 25, 2018, 19:51:51 pm »

Hello

Few days ago, I found a method in Circle that I never see before (certainly the fault of my glasses  Grin )

It allow to draw easily a surface object disk (works on 0.98 and 1.0)

++
David

Edit: mmm !.. I've a problem if I use the insert CODE or if I write the code as is ...
file attached

* circle3surface.zip (0.42 KB - downloaded 14 times.)
« Last Edit: April 25, 2018, 19:56:54 pm by dh42 » Logged
EddyCurrent
CNC Jedi
*****
Offline Offline

Posts: 3434



View Profile
« Reply #79 on: April 25, 2018, 20:25:51 pm »

David,

That works okay, thanks.
Logged
10bulls
Administrator
CNC Jedi
*****
Offline Offline

Posts: 2045


Coding Jedi


View Profile WWW
« Reply #80 on: April 26, 2018, 10:42:15 am »

Edit: mmm !.. I've a problem if I use the insert CODE or if I write the code as is ...
file attached

The degree symbol was causing a problem... database should be able to handle unicode characters now.

Code:
Sub main()

    'circle to surface
    
    Dim cir As Circle = New Circle 'a new circle primitive
    Dim cntr As Point3F = New Point3F(0,0,0)
    
    Dim surf as Surface = New Surface

    cir.Center = cntr

    cir.Diameter = val(inputbox("Diameter","Disk",1))

if cir.Diameter <= 0 then exit sub

    surf = cir.ToSurface(6)'  6 = angle of each step > 60 step to do 360°

    'add the objects to the document
    'CamBamUI.MainUI.ActiveView.CADFile.Add(cir)
    CamBamUI.MainUI.ActiveView.CADFile.Add(surf)

End Sub

⭐✌
« Last Edit: April 26, 2018, 11:10:34 am by 10bulls » Logged
Dragonfly
CNC Jedi
*****
Offline Offline

Posts: 1895



View Profile
« Reply #81 on: April 26, 2018, 13:35:26 pm »

Hi Andy & all
I want to write a script which will use a closed curve to determine whether a point (Point3F but only x/y coordinates used) is included in the area defined by the curve. And because I am at a very low level with CB API I don't know how to reference the curve (by its ID perhaps) and use it  to test each point while iterating through a selection of points.
Any hint, clue or link to info are welcome Smiley
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: 4966



View Profile WWW
« Reply #82 on: April 26, 2018, 15:52:23 pm »

Hello

You can use the PointInPolyline function (in polyline object) to test if a point is contained in a closed polyline.

Public Function PointInPolyline(ByVal p As Point2F, ByVal tolerance As Double) As Boolean.

Note that it's a Point2F, not a point3F, so you need to convert your point3F to Point2F

You can test a selection of object this way. (select a set of drawing objects)

Code:
Sub main()

    Dim p as Polyline

    If CamBamUI.MainUI.ActiveView.SelectedEntities.Length > 0 Then ' at least one entity selected

        For Each ent As Entity In CamBamUI.MainUI.ActiveView.SelectedEntities

            If TypeOf ent Is Polyline Then
                p = ent
                app.log("It's a polyline: ID = " & p.ID)
                ' it's a polyline
                ' do your tests here

            End If

        Next ent

    End If

End Sub
Logged
Dragonfly
CNC Jedi
*****
Offline Offline

Posts: 1895



View Profile
« Reply #83 on: April 26, 2018, 16:22:37 pm »

I was thinking about a method to directly reference the required polyline or make a clone object of it. But first I can isolate it from the selection because there is only one poly in it.
Thanks!  I know the method for testing inside or outside. Didn't know how to select the boundary polyline.
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: 4966



View Profile WWW
« Reply #84 on: April 26, 2018, 19:57:48 pm »

Hello

Find points contained in a polyline or polyrectangle

to use the following script: select a pointlist and a polyline (or a polyrectangle) then run the script.

the pointlist must be the first selected object in the selection ; polyline must be closed

the result is a new pointlist that contain only the points contained in the polyline.

Code:
Sub main()
    Dim first As Boolean = True
    Dim pl As PointList
    Dim target_pl As pointlist = New pointlist
    Dim poly As Polyline
    
    CamBamConfig.Defaults.ReloadTreeAfterScript = false 'select the object in the tree too ; for script only

    If view.SelectedEntities.Length = 2 Then

        For Each ent As Entity In View.SelectedEntities

            'first entry must be a pointlist
            If first = True Then
                If TypeOf ent Is PointList Then
                    pl = ent
                    first = False
                Else
                    MsgBox("first selected object must be a pointlist")
                    Exit Sub
                End If

            Else 'second entry after the point list

                If TypeOf ent Is Polyline Then
                    poly = ent
                    
                    if poly.closed = False then
                        MsgBox("the polyline must be closed")
                        Exit Sub
                    End If
                Else
                    MsgBox("second selected object must be a polyline or a polyrectangle")
                    Exit Sub
                End If
            End If
        Next ent
    Else
        msgbox("select a pointlist and a polyline or a polyrectangle")
    End If

    TestContained(poly, pl, target_pl)

End Sub

Sub TestContained(ByVal pl As Polyline, ByVal p As PointList, ByVal target As PointList)

    Dim pt2 As Point2F = New Point2F
    Dim newselection As ArrayList = New ArrayList()

    For Each pt As Point3F In p.Points 'for each point in the source pointlist
        pt2.X = pt.X 'convert to point2F
        pt2.Y = pt.Y
        If pl.PointInPolyline(pt2, 0.01) = True Then 'point contained
            target.Add(pt) 'add the contained point to the target pointlist
        End If
    Next pt

    If target.Points.Count = 0 Then
        MsgBox("no point contained in the polyline")
        Exit Sub
    End If
    
    MsgBox(target.Points.Count & " point(s) contained in the polyline")
    Doc.Add(target)
    newselection.Add(target)    'add the new pointlist to a selection
    CamBamUI.MainUI.ActiveView.Select(newselection)   'select the selection of shapes

    View.RefreshView()

End Sub

++
David
« Last Edit: April 26, 2018, 20:08:42 pm by dh42 » Logged
lloydsp
CNC Jedi
*****
Offline Offline

Posts: 7514



View Profile
« Reply #85 on: April 26, 2018, 21:00:59 pm »

Wow!  David, that was FAST!

Did you already have that script written, or did you do it in response to the post?

Either way, I applaud you! (but even more, if the latter)

Lloyd
Logged

"Pyro for Fun and Profit for More Than Fifty Years"
dh42
Administrator
CNC Jedi
*****
Offline Offline

Posts: 4966



View Profile WWW
« Reply #86 on: April 26, 2018, 21:14:09 pm »

Hello Lloyd

Quote
Did you already have that script written

No, I did it just now Wink (30 min of job)

++
David
Logged
lloydsp
CNC Jedi
*****
Offline Offline

Posts: 7514



View Profile
« Reply #87 on: April 26, 2018, 21:37:06 pm »

Congratulations!  You are now the "master" of CB scripts! <smile>

Lloyd
Logged

"Pyro for Fun and Profit for More Than Fifty Years"
Dragonfly
CNC Jedi
*****
Offline Offline

Posts: 1895



View Profile
« Reply #88 on: April 27, 2018, 13:10:08 pm »

Thanks again, David!
I save every piece of code you write and they are very useful for learning by example.
Although I try to convert them to Python as VBS won't run in Linux. Smiley
Here is what I got working for the moment. The script opens a file with point data, space delimited. Reads data line by line and checks whether the point is within the polyline boundary. If true it then adds the Point3F type to a PointList and at the same time writes the valid line to another file.
The counter printout at modulus 1000 is just to show me the script is alive Smiley

Code:
# read points from file
# check against a closed polyline area
# and add contained points to a poit list
#and at the same time write a text file with the valid points only

import sys
from CamBam import *
from CamBam.CAD import *
from CamBam.CAM import *
from CamBam.Geom import *

ascfile = "/home/ivan/temp/stop1-back.asc"                          # path and name of file.txt
transfile = "/home/ivan/temp/stop1-back-trim.asc"                          # path and name of file.txt


X=Y=Z = 0.0
QX=QY=QZ = 0.0
cnt = 0
pl = PointList()
p3 = Point3F()
p2 = Point2F()


if (view.SelectedEntities.Length == 1):
for ent in view.Selection:
 if (ent.GetType().Name == "Polyline"):
poly = Polyline(ent)
app.log("************************Polyline**************************")
app.log("Polyline:    "+ent.ID.ToString())

file1 = open(ascfile, "r")                                  # open the file read
file2 = open(transfile, "w")                                  # open the file write

for line in file1:
coordinates = line.split()
X,Y,Z = coordinates      # separate the coordinates
QX = float(X)
QY = float(Y)
QZ = float(Z)

p3 = Point3F(QX,QY,QZ)
p2 = Point2F(QX,QY)
if ( cnt % 1000 == 0):
app.log("Points:" + cnt.ToString())
#  PointInPolyline(ByVal p As Point2F, ByVal tolerance As Double) As Boolean.
if (QX > 0):
if poly.PointInPolyline(p2, 0.1):
pl.Add(QX,QY,QZ)
file2.write(line)

cnt += 1

file1.close()
file2.close()
doc.Add(pl)
else:
app.log("Error: Please select a single closed poly.")

Logged

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

Posts: 2415



View Profile
« Reply #89 on: April 27, 2018, 14:58:19 pm »

You are now the "master" of CB scripts!
***************
I always though so.. Wink
Logged

My 2¢
Pages: 1 ... 4 5 [6]
  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.371 seconds with 19 queries.

Copyright © 2018 HexRay Ltd. | Sitemap