Rabu, 17 Oktober 2012

Kumpulan Script Membuat 3D di PB 12.5 .NET dengan HELIX 3D TOOLKIT

Berikut kumpulan2 script dalam membuat aplikasi WPF 3D di Powerbuilder 12.NET, sebelum membuatnya ada baiknya anda download dulu komponen helix 3D toolkit disini
Selanjutnya add Reference file2 dll nya :
1. 3DTools.dll
2. HelixToolkit.Wpf.dll
3. HelisToolkit.Wpf.Input.dll

XAML :

 xmlns:helix="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"


<helix:HelixViewport3D Name="viewport3D"
InfoBackground="White"
Background="Transparent"
IsHeadLightEnabled="True"
InfiniteSpin="True"
CameraRotationMode="Turnball">
          <helix:HelixViewport3D.Camera>
                  <PerspectiveCamera Position="40,0,0"
LookDirection="-40,0,0"
UpDirection="0,0,9"
FieldOfView="61"
NearPlaneDistance="0.125"
FarPlaneDistance="Infinity" />
        </helix:HelixViewport3D.Camera>
            <helix:SunLight />
      </helix:HelixViewport3D>

       

SCRIPT :
1. Create Viewport3D dari Script
HelixToolkit.Wpf.HelixViewport3D viewport3D
viewport3D = create HelixToolkit.Wpf.HelixViewport3D
viewport3D.InfoBackground = System.Windows.Media.Brushes.Transparent
viewport3D.ZoomExtentsWhenLoaded = True
viewport3D.IsHeadLightEnabled = True
viewport3D.InfiniteSpin = True
viewport3D.Background = System.Windows.Media.Brushes.Transparent
viewport3D.CameraRotationMode = HelixToolkit.Wpf.CameraRotationMode.Turnball!

1. Memanggil File 3DS

HelixToolkit.Wpf.FileModelVisual3D iFile3D
iFile3D = Create HelixToolkit.Wpf.FileModelVisual3D
iFile3D.Source = "Files3D\heart.3DS"
viewport3D.Children.Add(iFile3D)

2. Rotate Object 3DS

System.Nullable lnull1,lnull2

lnull1 = create System.Nullable(1)
lnull2 = create System.Nullable(361)

System.Windows.Media.Animation.DoubleAnimation myAnimation
myAnimation = create System.Windows.Media.Animation.DoubleAnimation
myAnimation.From = lnull1
myAnimation.To = lnull2
myAnimation.Duration = create System.Windows.Duration(System.TimeSpan.FromMilliseconds(5000))
myAnimation.RepeatBehavior = System.Windows.Media.Animation.RepeatBehavior.Forever

System.Windows.Media.Media3D.Vector3D lvector3D
lvector3D = Create System.Windows.Media.Media3D.Vector3D(0, 0, 1)

System.Windows.Media.Media3D.AxisAngleRotation3D AxisAR3D
AxisAR3D = Create System.Windows.Media.Media3D.AxisAngleRotation3D(lvector3D,1)

System.Windows.Media.Media3D.RotateTransform3D myRotateTransform
myRotateTransform = Create System.Windows.Media.Media3D.RotateTransform3D(AxisAR3D)

myRotateTransform.Rotation.BeginAnimation(System.Windows.Media.Media3D.AxisAngleRotation3D.AngleProperty, myAnimation)

iFile3D.Transform = myRotateTransform

3. Menghentikan Rotate
System.Windows.Media.Media3D.RotateTransform3D myRotateTransform
myRotateTransform = Create System.Windows.Media.Media3D.RotateTransform3D

myRotateTransform.Rotation.Freeze()

iFile3D.Transform = myRotateTransform

4. Converting Warna

string ls_warna

ls_warna = '1'

choose case ls_warna
CASE '1'
ls_warna = '#2F3699'
CASE '2'
ls_warna = '#9DDA4E'
case '3'
ls_warna ='#00B7EF'
case '4'
ls_warna = '#000000'
case '5'
ls_warna = '#ED1C24'
case else
ls_warna = '#FFFFFF'
end choose

System.Windows.Media.BrushConverter lconverter
lconverter = create System.Windows.Media.BrushConverter

System.Windows.Media.Brush lbrush
lbrush = lconverter.ConvertFromString(ls_warna)

4. Menambahkan Object ke Viewport

HelixToolkit.Wpf.SphereVisual3D Sphere3D[1]
Sphere3D[1] = create HelixToolkit.Wpf.SphereVisual3D
Sphere3D[1].Radius = 2
Sphere3D[1].Center = p3
Sphere3D[1].Fill = lbrush
viewport3D.Children.Add(Sphere3D[1])

5. Merubah posisi Camera
System.Windows.Media.Media3D.PerspectiveCamera myPCamera
myPCamera = create System.Windows.Media.Media3D.PerspectiveCamera
myPCamera.NearPlaneDistance = 1
myPCamera.FieldOfView = 61
myPCamera.Position = Create System.Windows.Media.Media3D.Point3D(180,0,0)
myPCamera.LookDirection = Create System.Windows.Media.Media3D.Vector3D(-180,0,0)
myPCamera.UpDirection = create System.Windows.Media.Media3D.Vector3D(0, 0, 9)
viewport3D.Camera = myPCamera

6. Mencapture Viewport ke dalam Picture

System.Windows.Rect rect
rect = create System.Windows.Rect

rect.Height = view_3D.Viewport.ActualHeight
rect.Width  = view_3D.Viewport.ActualWidth

// Scale dimensions from 96 dpi to 600 dpi.
double scale

scale = 600 / 96

integer li_pixelWidth,li_pixelHeight

li_pixelWidth = integer(scale * (rect.Width + 1))
li_pixelHeight= integer(scale * (rect.Height + 1))

System.Windows.Media.Imaging.RenderTargetBitmap renderbitmap
renderbitmap = create System.Windows.Media.Imaging.RenderTargetBitmap(li_pixelWidth,li_pixelHeight,scale * 96,scale * 96, System.Windows.Media.PixelFormats.Default)
 
renderbitmap.Render(viewport3D)
 
    // set ke picture

p_1.Source = renderbitmap

//Jika ingin save ke File sekalian buat directorynya

string l_directori

l_directori = "C:\Temp_image\"

if System.IO.Directory.Exists(l_directori) then
l_directori = "C:\Temp_image\"
else
System.IO.Directory.CreateDirectory(l_directori)
end if

System.Windows.Media.Imaging.PngBitmapEncoder image
image = create System.Windows.Media.Imaging.PngBitmapEncoder

image.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(renderbitmap))

System.IO.Stream fs
fs = System.IO.File.Create(l_directori+"NamaFile.png")
image.Save(fs)


7. Save Viewport3D ke File
System.Windows.Controls.PrintDialog dlg
dlg  = create System.Windows.Controls.PrintDialog

if dlg.ShowDialog().GetValueOrDefault() = true then
dlg.PrintVisual(viewport3D.Viewport,"TEST")
end if