Author Topic: [VB script] Round the coordinates of the polyline points  (Read 10442 times)

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
[VB script] Round the coordinates of the polyline points
« on: September 16, 2013, 17:42:54 pm »
Hello,

This script allow to round the coordinates of the points in a polyline to the given number of decimals.

Only XYZ coordinates are rounded, the bulge value is not affected.

The number of decimals is set to 5 by default, but you can change it in the code ; change the value in the line:

   dim nbdeci as integer = 5

Only polylines are modified.

Code: [Select]
'Round the coordinates of points in a polyline
' to the given number of decimals.
' DH42 - 16/09/13 - V1.00

sub main

CamBamConfig.Defaults.ReloadTreeAfterScript = false

dim ent as Entity
dim pli as Polyline
dim plitem as PolylineItem
dim NewPoint as point3F
dim blg as double
dim nbdeci as integer = 5
dim ct as integer
dim nbpoints as integer
dim rep as string

app.Clearlog()

'ask for nb of decimals

rep = inputbox("Number of decimals","Round points coordinates",trim(str(nbdeci)))

if rep <> "" 'if not canceled

nbdeci=val(rep)

'if reply is out of range

if nbdeci < 0 or nbdeci > 15
msgbox("Nb of decimals must be between 0 - 15")
exit sub
end if

'scan selected entities

for each ent in view.SelectedEntities

if typeof ent is Polyline 'if polyline

pli = ent

if pli.Closed = True 'if polyline is closed nb points = nb segmentss - 1
nbpoints = pli.NumSegments-1
else
nbpoints = pli.NumSegments
end if


for ct = 0 to nbpoints 'scan all points in the polyline

plitem = pli.Points(ct)

' store the rounded values of the current point in a temp variable (Point3F)

NewPoint.x = Math.round(plitem.point.x,nbdeci)
NewPoint.y = Math.round(plitem.point.y,nbdeci)
NewPoint.z = Math.round(plitem.point.z,nbdeci)

' store the Bulge (not rounded)
blg = plitem.Bulge

'remove the current polylineitem
pli.Points.RemoveAt(ct)

' add the modified polylineitem at the same position
pli.InsertSegmentBefore(ct, new PolylineItem(NewPoint,blg))

next

end if

next

view.ClearSelections()
view.RefreshView()

end if

end sub

Thanks to pstemari for is help ;)

I (want) use this script when I've problem with leadmove (wrong direction), or missing toolpath in pockets, I discover that mostly, if I round the values of the points, the bug go away ... but it's long to do manually ... ;D

++
David
« Last Edit: September 16, 2013, 19:30:39 pm by dh42 »

Offline pixelmaker

  • CNC Jedi
  • *****
  • Posts: 1778
    • View Profile
    • pixelmaker
Re: [VB script] Round the coordinates of the polyline points
« Reply #1 on: September 17, 2013, 13:04:10 pm »
Hello David,
thank you for the script.

I never see that rounding the dezimal places brings a different if a mistake occures.
I try yesterday with tangent leadins and pockets to get a difference between the original file and used with the script.
Can you please upload a file with some mistakes that can be correct with the script.

ralf

Offline jeramenemafraise

  • Ewok
  • *
  • Posts: 8
    • View Profile
Re: [VB script] Round the coordinates of the polyline points
« Reply #2 on: September 17, 2013, 13:56:50 pm »
bonjour David,
J'ai fais un essai, mais je ne trouve pas d'arrondi dans les coordonnées X et Y de la polyligne que j'ai sélectionnée.
j'ai du faire une erreur ?
dommage car c'est un utilitaire sympa.
Yves.

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: [VB script] Round the coordinates of the polyline points
« Reply #3 on: September 17, 2013, 14:29:22 pm »
Hello David,
thank you for the script.

I never see that rounding the dezimal places brings a different if a mistake occures.
I try yesterday with tangent leadins and pockets to get a difference between the original file and used with the script.
Can you please upload a file with some mistakes that can be correct with the script.

ralf

Hello Ralf,

Unfortunately, I've not keep the files that cause pb, and I can't redo the bug ....(of course, never when I want)

++
David
« Last Edit: September 17, 2013, 14:34:07 pm by dh42 »

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: [VB script] Round the coordinates of the polyline points
« Reply #4 on: September 17, 2013, 14:33:16 pm »
bonjour David,
J'ai fais un essai, mais je ne trouve pas d'arrondi dans les coordonnées X et Y de la polyligne que j'ai sélectionnée.
j'ai du faire une erreur ?
dommage car c'est un utilitaire sympa.
Yves.

Hello,

http://screencast.com/t/JubXDLHxs

++
David

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: [VB script] Round the coordinates of the polyline points
« Reply #5 on: September 17, 2013, 15:31:52 pm »
Re

Quote
Can you please upload a file with some mistakes that can be correct with the script.

I try to retry the same job that give the lead bug, but nothing to do, that works each time ...  ::) I move the shape more and more ... and always no bug !!

We never find bug when we are looking for it, we find it only when work is in a rush !  ;D ;D

++
David

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: [VB script] Round the coordinates of the polyline points
« Reply #6 on: September 17, 2013, 15:53:54 pm »
Ah Ah !! I got it !!

see in attachment.

++
David

Offline pixelmaker

  • CNC Jedi
  • *****
  • Posts: 1778
    • View Profile
    • pixelmaker
Re: [VB script] Round the coordinates of the polyline points
« Reply #7 on: September 17, 2013, 19:32:01 pm »
Strange this.

If I drag the left "bad" rectangle with the mouse 0.05mm on x-axis to the left the lead in switches. But if I translate the same value with the transformation matrix it is still wrong.
It looks like a black hole in the drawing window. If I drag the rectangle in every direction a little bit the leadin works.
Thank you for this, I never see this before

ralf

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: [VB script] Round the coordinates of the polyline points
« Reply #8 on: September 17, 2013, 19:48:59 pm »
Another strange thing is also that in the real drawing there is 12 identical parts aligned, and all have the same problem until I change the two coordinates 36.4000007629395 by 36.4 (as in the shape at the right)