Tuesday, November 07, 2023

Placing Lines Within Polygons Uniquely

 I'll be honest, there might be a function to do this but I couldn't find it and it was only going to take a few minutes so let's take a look.  I have a bunch of triangles for and  XML file that need to be subdivided into areas that are defined by polygons.  I need to make sure that the triangles are kept intact and that there are no duplicates.  In my case it didn't need to be an exact science and I wasn't worried about the edges.  My solution was to check each triangle against a polygon and if the first point of the triangle was within the polygon it would be flagged, or in this case the layer changed.  I just banged it out and I may make changes but for now it does the trick.

print ('line_change_line_in_polygon.py modified   06:25 2023/11/07')
# Change layer of line that starts within polygon.

'''
Author: Dennis Shimer dshimer@gmail.com
License: cc-by-sa  http://creativecommons.org/licenses/by-sa/3.0/
'''

Ws=PyVrWs()
Line=PyVrLine()
BoundingLine=PyVrLine()
Gui=PyVrGui()

CheckLayer=600
NewLayer=1001
PointsFound=0
WsNum=Ws.Aws()

PromBox = VrPromBox ('Check lines in Polygon.', 30, 1)
PromBox.AddInt ('Layer to check', CheckLayer, 1, 32000)
PromBox.AddInt ('Change layer to', NewLayer, 1, 32000)
if (PromBox.Display(0) == 0):
CheckLayer=PromBox.GetIntByPrompt('Layer to check')
NewLayer=PromBox.GetIntByPrompt('Change layer to')
BoundingLine.Id()
print (CheckLayer,BoundingLine.GetLayer())
Gui.ProgInit('Lines',Ws.GetLineCount(WsNum))
for EntNum in range ( Ws.GetLineCount(WsNum)):
Gui.ProgSet(EntNum)
if CheckLayer == Ws.GetLineLayer(WsNum,EntNum):
Line.Load (WsNum, EntNum)
X,Y,Z=Line.GetPoint(0)
if BoundingLine.IsPointInside (X,Y):
# print (X,Y,Z)
PointsFound=PointsFound+1
Line.SetLayer(NewLayer)
Line.ReRec(1)
# Line.Plot()
Gui.ProgReset()
print (PointsFound)


 

For anyone interested in trying VrPython for the first time or if you are early in the game, I suggest going to the earliest posts and working forward. I use VrPython every day for many wonderful things, needless to say it will change and could potentially damage a file. Any risk associated with using VrPython or any code or scripts mentioned here lies solely with the end user.

The "Personal VrPython page" in the link section will contain many code examples and an organized table of contents to this blog in a fairly un-attractive (for now) form.