Snap-to script

This is a draft version of what I was talking about in the other thread (and like all my draft scripts will probably stay that way.

If you want to use it this is what it does. andnbsp;Select a point on an object and run the first script (after you save it to a file called something like ‘savepointto file.cs’). andnbsp;It saves the coordinates. andnbsp;Then select a point on a second object and run the second script (called ‘snaptofilecoordsblahblah.cs’) and it moves the second object so the points are aligned.

In this version the two objects have to part of the same group (couldn’t be bothered handling group orientation).

One andnbsp;use is like this – take a few faces from an object, cut and paste. andnbsp;Then I work on the new faces, then paste it back onto the object. andnbsp;Hope there’s room here for the code. andnbsp;Example fig attached.

Geo


first script


Language = VBScript

‘*********************************************************************************
‘ Purpose: put the coordinates of a point into a file – x, y and z on one line each with cr
‘*********************************************************************************

‘Option Explicit andnbsp; andnbsp; andnbsp; andnbsp; andnbsp;’require variable declarations

Sub Main (3DCApp)

andnbsp; andnbsp;Dim Scene
andnbsp; andnbsp;Dim ActivePointCount
andnbsp; andnbsp;Dim Group
andnbsp; andnbsp;Dim Object
‘ andnbsp; andnbsp;Dim Face
andnbsp; andnbsp;Dim FPoint
andnbsp; andnbsp;Dim gx,gy,gz andnbsp; ‘the group coordinates
andnbsp; andnbsp;Dim px,py,pz andnbsp; ‘the point coordinates
andnbsp; andnbsp;’variables for file handling
andnbsp; andnbsp;Dim fso, myfile

andnbsp; ‘get the scene
andnbsp; andnbsp;Set Scene = 3DCApp.GetActiveScene

andnbsp; andnbsp;’get the active object count
andnbsp; andnbsp;ActivePointCount = Scene.GetActivePointCount

andnbsp; andnbsp;’only proceed if there is an active object
andnbsp; andnbsp;If ActivePointCount [] 1 Then
andnbsp; andnbsp; andnbsp; andnbsp;MsgBox andquot;Please select just one point you git.andquot;
andnbsp; andnbsp;Else
andnbsp;
andnbsp;
andnbsp; andnbsp; ‘get the active object
andnbsp; andnbsp; andnbsp;Set Object = Scene.GetActiveObject(0)
andnbsp; andnbsp; andnbsp;Set Group = Object.GetParentGroup
andnbsp; andnbsp; andnbsp;Group.GetPosition nothing,gx,gy,gz andnbsp;
andnbsp; andnbsp; andnbsp;FPoint = Scene.GetActivePoint(0)
andnbsp; andnbsp; andnbsp;Object.GetPoint FPoint, px,py,pz
andnbsp; andnbsp; andnbsp;Set fso = CreateObject(andquot;Scripting.FileSystemObjectandquot;) andnbsp;
andnbsp; andnbsp; andnbsp;Set myfile = fso.CreateTextFile(andquot;c:Program FilesAmabilisScriptssnap_point.txtandquot;, True)
andnbsp; andnbsp; andnbsp;myfile.WriteLine(gx)
andnbsp; andnbsp; andnbsp;myfile.WriteLine(gy)
andnbsp; andnbsp; andnbsp;myfile.WriteLine(gz)
andnbsp; andnbsp; andnbsp;myfile.WriteLine(px)
andnbsp; andnbsp; andnbsp;myfile.WriteLine(py)
andnbsp; andnbsp; andnbsp;myfile.WriteLine(pz)
andnbsp; andnbsp; andnbsp;myfile.Close andnbsp;
andnbsp; end if
End Sub


second script


Language = VBScript

‘*********************************************************************************
‘ Purpose: gets the coordinates of a point from a file – x, y and z on one line each with cr
‘ andnbsp; andnbsp; andnbsp; andnbsp; andnbsp;gets points from new object and moves it according to the difference
‘ andnbsp; andnbsp; andnbsp; andnbsp; andnbsp;OBJECTS MUST BE IN THE SAME GROUP AS I’VE IGNORED GROUP ORIENTATION….
‘*********************************************************************************
‘ andnbsp;variable names with suffix 1 are TARGET
‘ andnbsp;variable names with suffix 2 are object being MOVED

‘Option Explicit andnbsp; andnbsp; andnbsp; andnbsp; andnbsp;’require variable declarations

Sub Main (3DCApp)

andnbsp; andnbsp;Dim Scene
andnbsp; andnbsp;Dim ActivePointCount
andnbsp; andnbsp;Dim Group1, Group2
andnbsp; andnbsp;Dim Object1, Object2
andnbsp; andnbsp;Dim NumPoints
andnbsp; andnbsp;Dim iii
‘ andnbsp; andnbsp;Dim Face
andnbsp; andnbsp;Dim FPoint1, FPoint2
andnbsp; andnbsp;Dim gx1,gy1,gz1 andnbsp; ‘the group coordinates of the target
andnbsp; andnbsp;Dim px1,py1,pz1 andnbsp; ‘the point coordinates of the target
andnbsp; andnbsp;Dim gx2,gy2,gz2 andnbsp; ‘the group coordinates of the object being moved
andnbsp; andnbsp;Dim px2,py2,pz2 andnbsp;
andnbsp; andnbsp;Dim MoveX, MoveY, MoveZ

andnbsp; andnbsp;’variables for file handling
andnbsp; andnbsp;Dim fso, myfile
andnbsp; andnbsp;Set fso = CreateObject(andquot;Scripting.FileSystemObjectandquot;) andnbsp;
andnbsp; andnbsp; andnbsp;Set myfile = fso.OpenTextFile(andquot;c:Program FilesAmabilisScriptssnap_point.txtandquot;)
andnbsp; andnbsp; ‘they are read as strings, so convert to double on the go
andnbsp; andnbsp; andnbsp;gx1=cdbl(myfile.ReadLine) andnbsp; andnbsp; andnbsp; andnbsp; andnbsp; andnbsp;’GROUP STUFF ISN’T USED IN THIS VERSION
andnbsp; andnbsp; andnbsp;gy1=cdbl(myfile.ReadLine)
andnbsp; andnbsp; andnbsp;gz1=cdbl(myfile.ReadLine)
andnbsp; andnbsp; andnbsp;px1=cdbl(myfile.ReadLine)
andnbsp; andnbsp; andnbsp;py1=cdbl(myfile.ReadLine)
andnbsp; andnbsp; andnbsp;pz1=cdbl(myfile.ReadLine)
andnbsp; andnbsp; andnbsp;myfile.Close andnbsp;

andnbsp; andnbsp; ‘get the scene
andnbsp; andnbsp; andnbsp;Set Scene = 3DCApp.GetActiveScene

andnbsp; andnbsp; ‘get the active object count
andnbsp; andnbsp; andnbsp;ActivePointCount = Scene.GetActivePointCount

andnbsp; andnbsp; ‘only proceed if there is an active object
andnbsp; andnbsp; If ActivePointCount [] 1 Then
andnbsp; andnbsp; andnbsp; andnbsp; MsgBox andquot;Please select just one point you git.andquot;
andnbsp; andnbsp; Else
andnbsp;
andnbsp;
andnbsp; andnbsp; ‘get the active object
andnbsp; andnbsp; andnbsp;Set Object2 = Scene.GetActiveObject(0)
andnbsp; andnbsp; andnbsp;NumPoints=Object2.GetPointCount()
andnbsp; andnbsp; andnbsp;Set Group2 = Object2.GetParentGroup
andnbsp; andnbsp; andnbsp;Group2.GetPosition nothing,gx2,gy2,gz2 andnbsp;
andnbsp; andnbsp; andnbsp;FPoint2 = Scene.GetActivePoint(0)
andnbsp; andnbsp; andnbsp;Object2.GetPoint FPoint2, px2,py2,pz2
andnbsp; andnbsp; ‘Now we just need to move the object according to the positions
andnbsp; andnbsp; ‘We’ll do it point by point assumiung they are in the same group. andnbsp;Lazy boy.

andnbsp; andnbsp; andnbsp; MoveX=px2-px1
andnbsp; andnbsp; andnbsp; MoveY=py2-py1
andnbsp; andnbsp; andnbsp; MoveZ=pz2-pz1 andnbsp;
andnbsp; andnbsp; andnbsp; for iii=0 to NumPoints-1
andnbsp; andnbsp; andnbsp; andnbsp; Object2.GetPoint iii,X,Y,Z
andnbsp; andnbsp; andnbsp; andnbsp; Object2.SetPoint iii, X-MoveX, Y-MoveY, Z-MoveZ
andnbsp; andnbsp; andnbsp; next
andnbsp; andnbsp; andnbsp;Object2.WriteScriptOperationLayer() andnbsp;
andnbsp; end if
End Sub


end script


You must be logged in to reply in this thread.

1 post