作业
This commit is contained in:
parent
ce867b4c7a
commit
19dab9dd80
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
20210129/demo - wpf/RenderApp.exe
(Stored with Git LFS)
BIN
20210129/demo - wpf/RenderApp.exe
(Stored with Git LFS)
Binary file not shown.
BIN
20210129/demo - wpf/clrcompression.dll
(Stored with Git LFS)
BIN
20210129/demo - wpf/clrcompression.dll
(Stored with Git LFS)
Binary file not shown.
BIN
20210129/demo - wpf/clrjit.dll
(Stored with Git LFS)
BIN
20210129/demo - wpf/clrjit.dll
(Stored with Git LFS)
Binary file not shown.
BIN
20210129/demo - wpf/coreclr.dll
(Stored with Git LFS)
BIN
20210129/demo - wpf/coreclr.dll
(Stored with Git LFS)
Binary file not shown.
BIN
20210129/demo - wpf/mscordaccore.dll
(Stored with Git LFS)
BIN
20210129/demo - wpf/mscordaccore.dll
(Stored with Git LFS)
Binary file not shown.
BIN
20210129/demo - wpf/vcruntime140_cor3.dll
(Stored with Git LFS)
BIN
20210129/demo - wpf/vcruntime140_cor3.dll
(Stored with Git LFS)
Binary file not shown.
BIN
20210129/demo - wpf/wpfgfx_cor3.dll
(Stored with Git LFS)
BIN
20210129/demo - wpf/wpfgfx_cor3.dll
(Stored with Git LFS)
Binary file not shown.
@ -1,16 +0,0 @@
|
||||
# 食用方法
|
||||
|
||||
1. 安装wamp程序,下载地址:[wamp](https://www.wampserver.com/)
|
||||
|
||||
2. 拷贝demo文件夹到wamp程序安装目录文件夹内的www下,例如:**H:\wamp64\www\demo**
|
||||
|
||||
3. 将自己的作业放置于**demo/res**文件夹下,single-demo.json是标准的文件,大家自己的作业用自己的名字缩写命名。
|
||||
|
||||
4. 修改**demo/js/main.js**下的20行,将single-demo.json修改为各自的json文件,即可在浏览器中间浏览。
|
||||
|
||||
5. 运行wamp程序,浏览器输入网址:[http://localhost/demo/index.html](http://localhost/demo/index.html)
|
||||
|
||||
6. 多个网格组成的构件,可以将构件的mesh值改为数组形式,里面放入多个网格位置索引
|
||||
|
||||
# Q&A记录
|
||||
|
@ -1,17 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>My first three.js app</title>
|
||||
<style>
|
||||
body { margin: 0; }
|
||||
canvas { display: block; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<script src="lib/three.js"></script>
|
||||
<script src="lib/OrbitControls.js"></script>
|
||||
<script src="js/loader.js"></script>
|
||||
<script src="js/main.js"></script>
|
||||
</body>
|
||||
</html>
|
@ -1,102 +0,0 @@
|
||||
BIMLoader = function(json)
|
||||
{
|
||||
// 解析材质
|
||||
var materials = new Array();
|
||||
json.materials.forEach(element => {
|
||||
var material = ParseMaterial(element);
|
||||
materials.push(material);
|
||||
});
|
||||
|
||||
// 解析网格
|
||||
var meshes = new Array();
|
||||
json.meshes.forEach(element => {
|
||||
var geometry = ParseGeometry(element);
|
||||
var material = materials[element.material];
|
||||
var mesh = new THREE.Mesh(geometry, material);
|
||||
meshes.push(mesh);
|
||||
});
|
||||
|
||||
// 解析物体
|
||||
var scene = new THREE.Scene();
|
||||
|
||||
json.instances.forEach(element => {
|
||||
var object = ParseInstance(element, meshes, element.mesh);
|
||||
scene.add(object);
|
||||
});
|
||||
|
||||
return scene;
|
||||
|
||||
function ParseMaterial(materialJson)
|
||||
{
|
||||
var material = new THREE.MeshPhongMaterial();
|
||||
|
||||
if(materialJson.color)
|
||||
material.color.setStyle(materialJson.color);
|
||||
|
||||
if(materialJson.transparent)
|
||||
{
|
||||
material.transparent = true;
|
||||
material.opacity = materialJson.transparent;
|
||||
}
|
||||
|
||||
return material;
|
||||
}
|
||||
|
||||
function ParseGeometry(geometryJson)
|
||||
{
|
||||
var geometry = new THREE.BufferGeometry();
|
||||
|
||||
geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(geometryJson.vertices), 3));
|
||||
|
||||
geometry.setIndex(new THREE.BufferAttribute(new Uint32Array(geometryJson.indices), 1));
|
||||
|
||||
geometry.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(geometryJson.normals), 3));
|
||||
|
||||
return geometry;
|
||||
}
|
||||
|
||||
function ParseInstance(instanceJson, meshes, index)
|
||||
{
|
||||
var obj = new THREE.Object3D();
|
||||
|
||||
if(instanceJson.id)
|
||||
obj.name = instanceJson.id;
|
||||
|
||||
if(instanceJson.transform)
|
||||
{
|
||||
var matrix4 = new THREE.Matrix4();
|
||||
matrix4.fromArray(instanceJson.transform);
|
||||
obj.matrixAutoUpdate = false;
|
||||
obj.matrix.copy(matrix4);
|
||||
obj.matrixWorldNeedsUpdate = true;
|
||||
}
|
||||
|
||||
if(instanceJson.visible)
|
||||
obj.visible = instanceJson.visible;
|
||||
|
||||
if(instanceJson.info)
|
||||
obj.userData = instanceJson.info;
|
||||
|
||||
if(Array.isArray(index))
|
||||
{
|
||||
index.forEach(element => {
|
||||
PutMesh(meshes[element], obj);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
PutMesh(meshes[index], obj);
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
function PutMesh(mesh, obj)
|
||||
{
|
||||
var edges = new THREE.EdgesGeometry(mesh.geometry);
|
||||
var line = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({color: 0xff0000}));
|
||||
|
||||
obj.add(mesh);
|
||||
obj.add(line);
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
// 场景、相机、光源定义定义
|
||||
var scene = new THREE.Scene();
|
||||
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000000);
|
||||
camera.up.set(0,0,1);
|
||||
var light = new THREE.AmbientLight(0xffffff);
|
||||
scene.add(light);
|
||||
|
||||
// 渲染器定义
|
||||
var canvas = document.createElement( 'canvas' );
|
||||
canvas.id = 'model';
|
||||
var context = canvas.getContext( 'webgl2', {antialias: true, preserveDrawingBuffer: true});
|
||||
var renderer = new THREE.WebGLRenderer({canvas: canvas, context: context, logarithmicDepthBuffer: true});
|
||||
renderer.setSize(window.innerWidth, window.innerHeight);
|
||||
renderer.setClearColor(0xffffff);
|
||||
document.body.appendChild(renderer.domElement);
|
||||
|
||||
window.addEventListener('resize', onWindowResize);
|
||||
|
||||
// 相机控制
|
||||
var controls = new THREE.OrbitControls(camera, renderer.domElement);
|
||||
|
||||
// 加载模型数据
|
||||
const loader = new THREE.FileLoader();
|
||||
loader.load(
|
||||
'/demo/res/single-demo.json',
|
||||
function(data)
|
||||
{
|
||||
var bimScene = BIMLoader(JSON.parse(data));
|
||||
var box = new THREE.Box3();
|
||||
box.expandByObject(bimScene);
|
||||
var center = new THREE.Vector3();
|
||||
box.getCenter(center);
|
||||
var size = new THREE.Vector3();
|
||||
box.getSize(size);
|
||||
var result = size.x > size.y ? size.x : size.y;
|
||||
result = result > size.z ? result : size.z;
|
||||
var maxLength = Math.round(result);
|
||||
var radius = center.distanceTo(new THREE.Vector3(center.x + (maxLength/2), center.y + (maxLength/2), center.z + (maxLength/2)));
|
||||
var cameraPos = new THREE.Vector3(center.x + radius * Math.cos(Math.PI/18) * Math.cos(Math.PI/4), center.y - radius * Math.cos(Math.PI/18) * Math.sin(Math.PI/4), center.z + radius * Math.sin(Math.PI/18));
|
||||
camera.position.copy(cameraPos);
|
||||
controls.target = box.getCenter();
|
||||
scene.add(bimScene);
|
||||
},
|
||||
function(xhr)
|
||||
{
|
||||
console.log((xhr.loaded / xhr.total * 100) + '% loaded');
|
||||
},
|
||||
function(err)
|
||||
{
|
||||
console.error('An error happened');
|
||||
}
|
||||
);
|
||||
|
||||
function onWindowResize()
|
||||
{
|
||||
camera.aspect = window.innerWidth / window.innerHeight;
|
||||
camera.updateProjectionMatrix();
|
||||
|
||||
renderer.setSize(window.innerWidth, window.innerHeight);
|
||||
}
|
||||
|
||||
function animate() {
|
||||
requestAnimationFrame( animate );
|
||||
controls.update();
|
||||
renderer.render( scene, camera );
|
||||
}
|
||||
|
||||
animate();
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@ -1,260 +0,0 @@
|
||||
{
|
||||
"instances": [
|
||||
{
|
||||
"id": "9039f77e-df87-4486-95c9-19715b85147d-00048b0b-caebf3df-1569-4ae2-93b2-149ceec97be9-00000019",
|
||||
"transform":[
|
||||
1.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
1.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
1.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
1.0
|
||||
],
|
||||
"visible": 1.0,
|
||||
"mesh": 0,
|
||||
"info":{
|
||||
"结构": "否",
|
||||
"启用分析模型": "否",
|
||||
"结构用途": "非承重",
|
||||
"定位线": "墙中心线",
|
||||
"图像": "<无>",
|
||||
"创建的阶段": "阶段 1",
|
||||
"拆除的阶段": "无",
|
||||
"底部约束": "标高 1",
|
||||
"底部偏移": "0",
|
||||
"已附着底部": "否",
|
||||
"底部延伸距离": "0",
|
||||
"顶部约束": "未连接",
|
||||
"无连接高度": "8000",
|
||||
"顶部偏移": "0",
|
||||
"已附着顶部": "否",
|
||||
"顶部延伸距离": "0",
|
||||
"房间边界": "是",
|
||||
"长度": "17000",
|
||||
"面积": "136 m²",
|
||||
"体积": "27.20 m³",
|
||||
"与体量相关": "否",
|
||||
"Type 在插入点包络": "不包络",
|
||||
"Type 在端点包络": "无",
|
||||
"Type 厚度": "200",
|
||||
"Type 类型图像": "<无>",
|
||||
"Type 结构材质": "<按类别>",
|
||||
"Type 成本": "0.00",
|
||||
"Type 功能": "外部",
|
||||
"Type 传热系数(U)": "0.0000 W/(m²·K)",
|
||||
"Type 热阻(R)": "0.0000 (m²·K)/W",
|
||||
"Type 热质量": "0.00 kJ/K",
|
||||
"Type 吸收率": "0.7",
|
||||
"Type 粗糙度": "3",
|
||||
"revit_id": "9039f77e-df87-4486-95c9-19715b85147d-00048b0b"
|
||||
}
|
||||
}
|
||||
],
|
||||
"meshes": [
|
||||
{
|
||||
"vertices":[
|
||||
-8921.5,
|
||||
-8860.90137,
|
||||
8000,
|
||||
-9121.22559,
|
||||
-8871.36914,
|
||||
8000,
|
||||
-9121.22559,
|
||||
-8871.36914,
|
||||
0,
|
||||
-8921.5,
|
||||
-8860.90137,
|
||||
0,
|
||||
-9811.21094,
|
||||
8115.80029,
|
||||
8000,
|
||||
-8921.5,
|
||||
-8860.90137,
|
||||
8000,
|
||||
-8921.5,
|
||||
-8860.90137,
|
||||
0,
|
||||
-9811.21094,
|
||||
8115.80029,
|
||||
0,
|
||||
-10010.9365,
|
||||
8105.3335,
|
||||
8000,
|
||||
-9811.21094,
|
||||
8115.80029,
|
||||
8000,
|
||||
-9811.21094,
|
||||
8115.80029,
|
||||
0,
|
||||
-10010.9365,
|
||||
8105.3335,
|
||||
0,
|
||||
-9121.22559,
|
||||
-8871.36914,
|
||||
8000,
|
||||
-10010.9365,
|
||||
8105.3335,
|
||||
8000,
|
||||
-10010.9365,
|
||||
8105.3335,
|
||||
0,
|
||||
-9121.22559,
|
||||
-8871.36914,
|
||||
0,
|
||||
-9811.21094,
|
||||
8115.80029,
|
||||
0,
|
||||
-8921.5,
|
||||
-8860.90137,
|
||||
0,
|
||||
-9121.22559,
|
||||
-8871.36914,
|
||||
0,
|
||||
-10010.9365,
|
||||
8105.3335,
|
||||
0,
|
||||
-10010.9365,
|
||||
8105.3335,
|
||||
8000,
|
||||
-9121.22559,
|
||||
-8871.36914,
|
||||
8000,
|
||||
-8921.5,
|
||||
-8860.90137,
|
||||
8000,
|
||||
-9811.21094,
|
||||
8115.80029,
|
||||
8000
|
||||
],
|
||||
"indices":[
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
0,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
4,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
10,
|
||||
8,
|
||||
10,
|
||||
11,
|
||||
12,
|
||||
13,
|
||||
14,
|
||||
12,
|
||||
14,
|
||||
15,
|
||||
16,
|
||||
17,
|
||||
18,
|
||||
16,
|
||||
18,
|
||||
19,
|
||||
20,
|
||||
21,
|
||||
22,
|
||||
20,
|
||||
22,
|
||||
23
|
||||
],
|
||||
"normals":[
|
||||
0.0523389392,
|
||||
-0.998629391,
|
||||
0,
|
||||
0.0523389392,
|
||||
-0.998629391,
|
||||
0,
|
||||
0.0523389392,
|
||||
-0.998629391,
|
||||
0,
|
||||
0.0523389392,
|
||||
-0.998629391,
|
||||
0,
|
||||
0.99862951,
|
||||
0.0523359366,
|
||||
0,
|
||||
0.99862951,
|
||||
0.0523359366,
|
||||
0,
|
||||
0.99862951,
|
||||
0.0523359366,
|
||||
0,
|
||||
0.99862951,
|
||||
0.0523359366,
|
||||
0,
|
||||
-0.0523340702,
|
||||
0.99862963,
|
||||
0,
|
||||
-0.0523340702,
|
||||
0.99862963,
|
||||
0,
|
||||
-0.0523340702,
|
||||
0.99862963,
|
||||
0,
|
||||
-0.0523340702,
|
||||
0.99862963,
|
||||
0,
|
||||
-0.99862951,
|
||||
-0.0523359329,
|
||||
0,
|
||||
-0.99862951,
|
||||
-0.0523359329,
|
||||
0,
|
||||
-0.99862951,
|
||||
-0.0523359329,
|
||||
0,
|
||||
-0.99862951,
|
||||
-0.0523359329,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
-1,
|
||||
0,
|
||||
0,
|
||||
-1,
|
||||
0,
|
||||
0,
|
||||
-1,
|
||||
0,
|
||||
0,
|
||||
-1,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
0,
|
||||
0,
|
||||
1
|
||||
],
|
||||
"material": 0
|
||||
}
|
||||
],
|
||||
"materials": [
|
||||
{
|
||||
"color": "#808080",
|
||||
"transparent": 1.0
|
||||
}
|
||||
]
|
||||
}
|
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 159 KiB |
Binary file not shown.
Before Width: | Height: | Size: 8.9 KiB |
64
20210129/zzx-20210129作业/Data/Instance.cs
Normal file
64
20210129/zzx-20210129作业/Data/Instance.cs
Normal file
@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
public class Instance
|
||||
{
|
||||
string id;
|
||||
double[] transform;
|
||||
double visible;
|
||||
int mesh;
|
||||
List<string> info;
|
||||
|
||||
/// <summary>
|
||||
/// 构件的id
|
||||
/// </summary>
|
||||
public string Id { get => id; set => id = value; }
|
||||
/// <summary>
|
||||
/// 变换矩阵
|
||||
/// </summary>
|
||||
public double[] Transform { get => transform; set => transform = value; }
|
||||
/// <summary>
|
||||
/// 可见性
|
||||
/// </summary>
|
||||
public double Visible { get => visible; set => visible = value; }
|
||||
/// <summary>
|
||||
/// 对应网格数组中的序号
|
||||
/// </summary>
|
||||
public int Mesh { get => mesh; set => mesh = value; }
|
||||
/// <summary>
|
||||
/// 对应材质数组中的序号
|
||||
/// </summary>
|
||||
public List<string> Info { get => info; set => info = value; }
|
||||
|
||||
public Instance()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public string ToJson()
|
||||
{
|
||||
string result = null;
|
||||
result = string.Format
|
||||
(
|
||||
"\n \"id\":{0},"
|
||||
+ "\n \"transform\":[{1}],"
|
||||
+ "\n \"visible\":{2},"
|
||||
+ "\n \"mesh\":{3},"
|
||||
+ "\n \"info\":{{{4}\n}}",
|
||||
id,
|
||||
string.Join(",\n",transform.Select<double,string>(x=>x.ToString("f1"))),
|
||||
visible.ToString("f1"),
|
||||
mesh,
|
||||
string.Join(",\n", info)
|
||||
);
|
||||
|
||||
return "\n{" + result + "\n}";
|
||||
}
|
||||
}
|
||||
}
|
71
20210129/zzx-20210129作业/Data/MaterialData.cs
Normal file
71
20210129/zzx-20210129作业/Data/MaterialData.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
class MaterialData
|
||||
{
|
||||
public List<Color> InstanceColorList { get; set; }
|
||||
|
||||
public List<double> TransparentList { get; set; }
|
||||
|
||||
public MaterialData()
|
||||
{
|
||||
InstanceColorList = new List<Color>();
|
||||
TransparentList = new List<double>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加颜色
|
||||
/// </summary>
|
||||
/// <param name="c"></param>
|
||||
/// <param name="transparent"></param>
|
||||
/// <returns>颜色的索引</returns>
|
||||
public int AddColor(Color c,double transparent)
|
||||
{
|
||||
int index = 0;
|
||||
int findIndex = InstanceColorList.FindIndex(x => x.Red==c.Red&&x.Green==c.Green&&x.Blue==c.Blue);
|
||||
if(findIndex != -1&& TransparentList[findIndex]==transparent)
|
||||
{
|
||||
index = findIndex;
|
||||
}
|
||||
else
|
||||
{
|
||||
InstanceColorList.Add(c);
|
||||
TransparentList.Add(transparent);
|
||||
index = InstanceColorList.Count - 1;
|
||||
}
|
||||
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
public string ToJosn()
|
||||
{
|
||||
string result = null;
|
||||
for (int i = 0; i < InstanceColorList.Count; i++)
|
||||
{
|
||||
if (i == InstanceColorList.Count - 1)
|
||||
{
|
||||
result += $"\n{{\n\"color\":\"{colorRGBtoHx16(InstanceColorList[i])}\",\n\"transparent\":{TransparentList[i]}}}";
|
||||
}
|
||||
else
|
||||
{
|
||||
result += $"\n{{\n\"color\":\"{colorRGBtoHx16(InstanceColorList[i])}\",\n\"transparent\":{TransparentList[i]}}},";
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public string colorRGBtoHx16(Color c)
|
||||
{
|
||||
|
||||
return System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(c.Red,c.Green, c.Blue));
|
||||
}
|
||||
}
|
||||
}
|
111
20210129/zzx-20210129作业/Data/MeshData.cs
Normal file
111
20210129/zzx-20210129作业/Data/MeshData.cs
Normal file
@ -0,0 +1,111 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
class MeshData
|
||||
{
|
||||
#region 数据
|
||||
|
||||
/// <summary>
|
||||
/// 顶点集合
|
||||
/// </summary>
|
||||
public List<XYZ> VerticeList { get; set; }
|
||||
/// <summary>
|
||||
/// 面索引集合
|
||||
/// </summary>
|
||||
public List<int> IndiceList { get; set; }
|
||||
/// <summary>
|
||||
/// 顶点法向量集合
|
||||
/// </summary>
|
||||
public List<XYZ> VerticeNormalList { get; set; }
|
||||
/// <summary>
|
||||
/// 对应材质数组编号
|
||||
/// </summary>
|
||||
public int MaterialIndex { get; set; }
|
||||
#endregion
|
||||
|
||||
#region 构造函数
|
||||
public MeshData()
|
||||
{
|
||||
VerticeList = new List<XYZ>();
|
||||
IndiceList = new List<int>();
|
||||
VerticeNormalList = new List<XYZ>();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 方法
|
||||
/// <summary>
|
||||
/// 把顶点信息添加到集合中,然后返回对应的点在顶点集合中的索引
|
||||
/// </summary>
|
||||
/// <param name="meshVerticeList"></param>
|
||||
/// <returns></returns>
|
||||
public List<int> AddVertice(IList<XYZ> meshVerticeList, XYZ faceNormal)
|
||||
{
|
||||
List<int> verticelIndexList = new List<int>();
|
||||
foreach (XYZ meshVertice in meshVerticeList)
|
||||
{
|
||||
int index = VerticeList.FindIndex(x => x.DistanceTo(meshVertice) < 1.ToFeet());
|
||||
if (index == -1)
|
||||
{
|
||||
VerticeList.Add(meshVertice);
|
||||
VerticeNormalList.Add(faceNormal);
|
||||
verticelIndexList.Add(VerticeList.Count() - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
verticelIndexList.Add(index);
|
||||
//法向量相加再单位化
|
||||
if (VerticeNormalList[index].AngleTo(faceNormal) < 91.ToRad())
|
||||
{
|
||||
VerticeNormalList[index] = (faceNormal + VerticeNormalList[index]).Normalize();
|
||||
}
|
||||
}
|
||||
}
|
||||
return verticelIndexList;
|
||||
}
|
||||
|
||||
public string ToJosn()
|
||||
{
|
||||
string result = null;
|
||||
result = string.Format
|
||||
(
|
||||
"\n \"vertices\":[\n{0}\n],"
|
||||
+ "\n \"indices\":[\n{1}\n],"
|
||||
+ "\n \"normals\":[\n{2}\n],"
|
||||
+ "\n \"material\":{3}",
|
||||
string.Join(",\n", VerticeList.Select<XYZ, string>(x => XYZTransform(x,false))),
|
||||
string.Join(",\n", IndiceList.Select<int, string>(x => x.ToString())),
|
||||
string.Join(",\n", VerticeNormalList.Select<XYZ, string>(x => XYZTransform(x))),
|
||||
MaterialIndex
|
||||
);
|
||||
|
||||
return "\n{" + result + "\n}";
|
||||
}
|
||||
|
||||
|
||||
|
||||
private string XYZTransform(XYZ point,bool isDirection=true)
|
||||
{
|
||||
string result = null;
|
||||
if (isDirection)
|
||||
{
|
||||
point = point.Normalize();
|
||||
result = $"{point.X.Round(0)},\n{point.Y.Round(0)},\n{point.Z.Round(0)}";
|
||||
}
|
||||
else
|
||||
{
|
||||
result = $"{point.X.ToMM().Round(0)},\n{point.Y.ToMM().Round(0)},\n{point.Z.ToMM().Round(0)}";
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
90
20210129/zzx-20210129作业/GraphicsStudy.csproj
Normal file
90
20210129/zzx-20210129作业/GraphicsStudy.csproj
Normal file
@ -0,0 +1,90 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{8B9832C5-1479-4C32-A7DA-4B9B06880158}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>GraphicsStudy</RootNamespace>
|
||||
<AssemblyName>GraphicsStudy</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="NewtonSoft.Json">
|
||||
<HintPath>C:\Program Files\Autodesk\Revit 2019\NewtonSoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="PresentationFramework" />
|
||||
<Reference Include="RevitAPI">
|
||||
<HintPath>C:\Program Files\Autodesk\Revit 2019\RevitAPI.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="RevitAPIUI">
|
||||
<HintPath>C:\Program Files\Autodesk\Revit 2019\RevitAPIUI.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Data\Test\BeamData.cs" />
|
||||
<Compile Include="Data\Test\ColumnData.cs" />
|
||||
<Compile Include="Data\Test\CTData.cs" />
|
||||
<Compile Include="Data\Test\DataInfo.cs" />
|
||||
<Compile Include="Data\Test\DuctData.cs" />
|
||||
<Compile Include="Data\Test\FloorData.cs" />
|
||||
<Compile Include="Data\Test\PipeData.cs" />
|
||||
<Compile Include="Data\Test\WallData.cs" />
|
||||
<Compile Include="DeleteTest.cs" />
|
||||
<Compile Include="GetTest.cs" />
|
||||
<Compile Include="GraphicsStudyCmd.cs" />
|
||||
<Compile Include="Data\Instance.cs" />
|
||||
<Compile Include="json\ElementIdConverter.cs" />
|
||||
<Compile Include="json\JsonUtils.cs" />
|
||||
<Compile Include="json\LineConverter.cs" />
|
||||
<Compile Include="json\TransformConvert.cs" />
|
||||
<Compile Include="json\XYZConverter.cs" />
|
||||
<Compile Include="Data\MaterialData.cs" />
|
||||
<Compile Include="Method\DocumentEx.cs" />
|
||||
<Compile Include="Method\DoubleMethod.cs" />
|
||||
<Compile Include="Method\GeoMethod.cs" />
|
||||
<Compile Include="Method\LineListMethod.cs" />
|
||||
<Compile Include="Method\LineMethod.cs" />
|
||||
<Compile Include="Method\TestMethod.cs" />
|
||||
<Compile Include="Method\VectorsMethod.cs" />
|
||||
<Compile Include="Method\ViewMethod.cs" />
|
||||
<Compile Include="Method\WallMethod.cs" />
|
||||
<Compile Include="Method\WindowMethod.cs" />
|
||||
<Compile Include="Method\XYZMethod.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Data\MeshData.cs" />
|
||||
<Compile Include="SetTest.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
25
20210129/zzx-20210129作业/GraphicsStudy.sln
Normal file
25
20210129/zzx-20210129作业/GraphicsStudy.sln
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30204.135
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GraphicsStudy", "GraphicsStudy.csproj", "{8B9832C5-1479-4C32-A7DA-4B9B06880158}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{8B9832C5-1479-4C32-A7DA-4B9B06880158}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{8B9832C5-1479-4C32-A7DA-4B9B06880158}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8B9832C5-1479-4C32-A7DA-4B9B06880158}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8B9832C5-1479-4C32-A7DA-4B9B06880158}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {5AFA9915-ACE3-46CC-BE6B-AF5EF11C5DE5}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
148
20210129/zzx-20210129作业/GraphicsStudyCmd.cs
Normal file
148
20210129/zzx-20210129作业/GraphicsStudyCmd.cs
Normal file
@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Autodesk.Revit.DB;
|
||||
using Autodesk.Revit.UI;
|
||||
using Autodesk.Revit.Attributes;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
[Transaction(TransactionMode.Manual)]
|
||||
public class GraphicsStudyCmd : IExternalCommand
|
||||
{
|
||||
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
|
||||
{
|
||||
UIDocument uidoc = commandData.Application.ActiveUIDocument;
|
||||
Document doc = uidoc.Document;
|
||||
string path = @"D:\phpstudy_pro\WWW\demo\res\ceshi22.json";
|
||||
StreamWriter sw = new StreamWriter(path);
|
||||
|
||||
List<Reference> selRefList;
|
||||
//try
|
||||
//{
|
||||
// selRefList = uidoc.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element).ToList();
|
||||
//}
|
||||
//catch
|
||||
//{
|
||||
// return Result.Succeeded;
|
||||
//}
|
||||
|
||||
//FamilyInstance fi = doc.GetElement(selRefList[0]) as FamilyInstance;
|
||||
|
||||
//BeamData bd = new BeamData(doc, doc.ActiveView.GenLevel, 3500.ToFeet(), (fi.Location as LocationCurve).Curve as Line, fi.Symbol);
|
||||
//bd.Width = 100;
|
||||
//bd.Heigth = 500;
|
||||
//bd.Number = "D1111";
|
||||
Transaction trans = new Transaction(doc, "123");
|
||||
trans.Start();
|
||||
string ddd = "1E88AB71-D7B1-4BE4-A531-09DBE9F62A8E";
|
||||
//bool b = doc.SetData<string>(ddd, "ceshi", bd.ToJson());
|
||||
//doc.DeleteData(ddd);
|
||||
string b = doc.GetData<string>(ddd, "ceshi");
|
||||
string[] lineArray = b.Split(new string[] { "\r\n" }, StringSplitOptions.None);
|
||||
BeamData nwBD = new BeamData();
|
||||
nwBD.GetJson(lineArray);
|
||||
trans.Commit();
|
||||
MessageBox.Show(b.ToString());
|
||||
MessageBox.Show(nwBD.Number);
|
||||
return Result.Succeeded;
|
||||
|
||||
List<Instance> instanceList = new List<Instance>();
|
||||
List<MeshData> mdList = new List<MeshData>();
|
||||
MaterialData materData = new MaterialData();
|
||||
|
||||
|
||||
for (int ii = 0; ii < selRefList.Count; ii++)
|
||||
{
|
||||
Reference selRef = selRefList[ii];
|
||||
Element selElem = doc.GetElement(selRef);
|
||||
//设置instance
|
||||
Instance ins = new Instance();
|
||||
ins.Id = $"\"{selElem.UniqueId}\"";
|
||||
ins.Mesh = ii;
|
||||
ins.Visible = 1.0;
|
||||
ins.Transform = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
|
||||
ins.Info = new List<string>() { $"\n\"名字\":\"{selElem.Name}\"" };
|
||||
instanceList.Add(ins);
|
||||
//设置mesh
|
||||
var geo = selElem.get_Geometry(new Options());
|
||||
List<Solid> solidList = GeoMethod.GetSolids(geo);
|
||||
MeshData md = new MeshData();
|
||||
|
||||
foreach (var s in solidList)
|
||||
{
|
||||
foreach (var solidFace in s.Faces)
|
||||
{
|
||||
PlanarFace planarFace = solidFace as PlanarFace;
|
||||
ElementId materId = planarFace.MaterialElementId;
|
||||
|
||||
if (materId != ElementId.InvalidElementId&&materId!=null)
|
||||
{
|
||||
Material faceMaterial = doc.GetElement(materId) as Material;
|
||||
md.MaterialIndex= materData.AddColor(faceMaterial.Color, faceMaterial.Transparency);
|
||||
}
|
||||
//得到面的网格
|
||||
Mesh faceMesh = planarFace.Triangulate();
|
||||
//得到面的顶点在总的顶点中的索引
|
||||
List<int> meshVIndexList = md.AddVertice(faceMesh.Vertices, planarFace.FaceNormal);
|
||||
//遍历三角面片
|
||||
for (int i = 0; i < faceMesh.NumTriangles; i++)
|
||||
{
|
||||
MeshTriangle mt = faceMesh.get_Triangle(i);
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
int index = (int)mt.get_Index(j);
|
||||
md.IndiceList.Add(meshVIndexList[index]);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
md.MaterialIndex = ii;
|
||||
mdList.Add(md);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
//输出数据
|
||||
sw.Write("{\n");
|
||||
sw.Write("\"instances\":[");
|
||||
instanceList.ForEach(x => sw.Write(x.ToJson()));
|
||||
sw.Write("\n],");
|
||||
sw.Write("\"meshes\":[");
|
||||
mdList.ForEach(md => sw.Write(md.ToJosn()));
|
||||
sw.Write("\n],");
|
||||
sw.Write("\"materials\":[");
|
||||
sw.Write(materData.ToJosn());
|
||||
sw.Write("\n]");
|
||||
sw.Write("\n}");
|
||||
sw.Close();
|
||||
MessageBox.Show("生成成功");
|
||||
|
||||
#region 格式测试
|
||||
//Instance i = new Instance();
|
||||
//i.Id = 12345;
|
||||
//i.Mesh = 0;
|
||||
//i.Info = 1;
|
||||
//i.Transform = new int[]{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
|
||||
//i.Visible = 0.5;
|
||||
//string iToJosn = i.ToJson();
|
||||
//string path = @"C:\Users\UBIMC-104\Desktop\josn导出\ceshi.json";
|
||||
//StreamWriter sw = new StreamWriter(path);
|
||||
//sw.Write("\"instances\":[");
|
||||
//sw.Write(iToJosn);
|
||||
//sw.Write("\r\n],");
|
||||
//sw.Close();
|
||||
#endregion
|
||||
|
||||
|
||||
return Result.Succeeded;
|
||||
}
|
||||
}
|
||||
}
|
22
20210129/zzx-20210129作业/Method/DoubleMethod.cs
Normal file
22
20210129/zzx-20210129作业/Method/DoubleMethod.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
public static class DoubleMethod
|
||||
{
|
||||
/// <summary>
|
||||
/// 四舍五入,保留几位小数点
|
||||
/// </summary>
|
||||
/// <param name="d1"></param>
|
||||
/// <param name="i"></param>
|
||||
/// <returns></returns>
|
||||
public static double Round(this double d1, int i = 0)
|
||||
{
|
||||
return Math.Round(d1, i);
|
||||
}
|
||||
}
|
||||
}
|
248
20210129/zzx-20210129作业/Method/GeoMethod.cs
Normal file
248
20210129/zzx-20210129作业/Method/GeoMethod.cs
Normal file
@ -0,0 +1,248 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
public static class GeoMethod
|
||||
{
|
||||
/// <summary>
|
||||
/// 得到墙的最底面的线
|
||||
/// </summary>
|
||||
/// <param name="geo"></param>
|
||||
/// <returns></returns>
|
||||
public static List<Autodesk.Revit.DB.Line> GetGeoEleDownLine(GeometryElement geo)
|
||||
{
|
||||
List<Autodesk.Revit.DB.Line> geoLineList = new List<Autodesk.Revit.DB.Line>();
|
||||
foreach (GeometryObject geoObject in geo)
|
||||
{
|
||||
Solid geoSolid = geoObject as Solid;
|
||||
foreach (Face geoFace in geoSolid.Faces)
|
||||
{
|
||||
PlanarFace temFace = geoFace as PlanarFace;
|
||||
if (Math.Abs(temFace.FaceNormal.X) < 0.01 && Math.Abs(temFace.FaceNormal.Y) < 0.01 && temFace.FaceNormal.Z < 0)
|
||||
{
|
||||
foreach (EdgeArray eArr in temFace.EdgeLoops)
|
||||
{
|
||||
foreach (Edge e in eArr)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (e.AsCurve() is Autodesk.Revit.DB.Line l1)
|
||||
{
|
||||
geoLineList.Add(l1.SetZ());
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return geoLineList.JoinLines(0.01);
|
||||
}
|
||||
/// <summary>
|
||||
/// 得到墙的最底面的面
|
||||
/// </summary>
|
||||
/// <param name="geo"></param>
|
||||
/// <returns></returns>
|
||||
public static PlanarFace GetGeoEleDownFace(GeometryElement geo)
|
||||
{
|
||||
PlanarFace face = null;
|
||||
foreach (GeometryObject geoObject in geo)
|
||||
{
|
||||
Solid geoSolid = geoObject as Solid;
|
||||
foreach (Face geoFace in geoSolid.Faces)
|
||||
{
|
||||
PlanarFace temFace = geoFace as PlanarFace;
|
||||
if (Math.Abs(temFace.FaceNormal.X) < 0.01 && Math.Abs(temFace.FaceNormal.Y) < 0.01 && temFace.FaceNormal.Z < 0)
|
||||
{
|
||||
face = temFace;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return face;
|
||||
}
|
||||
/// <summary>
|
||||
/// 得到集合体所有的面
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
public static List<Face> GetSolidAllFace(this Solid s)
|
||||
{
|
||||
List<Face> allFace = new List<Face>();
|
||||
foreach (Face f in s.Faces)
|
||||
{
|
||||
allFace.Add(f);
|
||||
}
|
||||
return allFace;
|
||||
}
|
||||
/// <summary>
|
||||
/// 计算几何体的表面积
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
public static double CalculatedSurfaceArea(this Solid s)
|
||||
{
|
||||
double surfaceArea = 0;
|
||||
List<Face> allFace = s.GetSolidAllFace();
|
||||
foreach (Face f in allFace)
|
||||
{
|
||||
surfaceArea += f.Area;
|
||||
}
|
||||
return surfaceArea;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回几何体的最底面
|
||||
/// </summary>
|
||||
/// <param name="solid"></param>
|
||||
/// <returns></returns>
|
||||
public static Face GetSolidDownFace(this Solid solid)
|
||||
{
|
||||
Face face = null;
|
||||
foreach (Face geoFace in solid.Faces)
|
||||
{
|
||||
PlanarFace temFace = geoFace as PlanarFace;
|
||||
if (Math.Abs(temFace.FaceNormal.X) < 0.01 && Math.Abs(temFace.FaceNormal.Y) < 0.01 && temFace.FaceNormal.Z < 0)
|
||||
{
|
||||
face = temFace;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return face;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取构件集合的Solids集合
|
||||
/// </summary>
|
||||
/// <param name="elements">构件集合</param>
|
||||
/// <returns></returns>
|
||||
public static List<Solid> GetSolidByElements(this List<Element> elements)
|
||||
{
|
||||
List<Solid> result = new List<Solid>();
|
||||
Options options = new Options();
|
||||
elements.ForEach(e =>
|
||||
{
|
||||
GeometryElement geometryElement = e.get_Geometry(options);
|
||||
List<Solid> solids = GetSolids(geometryElement);
|
||||
if (solids.Count > 0)
|
||||
{
|
||||
result.AddRange(solids);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有的Solid
|
||||
/// </summary>
|
||||
/// <param name="geometryElement"></param>
|
||||
/// <returns></returns>
|
||||
public static List<Solid> GetSolids(GeometryElement geometryElement)
|
||||
{
|
||||
List<Solid> result = new List<Solid>();
|
||||
foreach (GeometryObject geomObj in geometryElement)
|
||||
{
|
||||
Solid solid = geomObj as Solid;
|
||||
if (null != solid)
|
||||
{
|
||||
result.Add(solid);
|
||||
continue;
|
||||
}
|
||||
//If this GeometryObject is Instance, call AddCurvesAndSolids
|
||||
GeometryInstance geomInst = geomObj as GeometryInstance;
|
||||
if (null != geomInst)
|
||||
{
|
||||
GeometryElement transformedGeomElem = geomInst.GetInstanceGeometry(geomInst.Transform);
|
||||
result.AddRange(GetSolids(transformedGeomElem));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Solid布尔操作
|
||||
/// </summary>
|
||||
/// <param name="solidA"></param>
|
||||
/// <param name="solidB"></param>
|
||||
/// <param name="booleanOperationsType"></param>
|
||||
/// <returns></returns>
|
||||
public static Solid SolidBooleanOperation(Solid solidA, Solid solidB, BooleanOperationsType booleanOperationsType)
|
||||
{
|
||||
Solid result = null;
|
||||
try
|
||||
{
|
||||
result = BooleanOperationsUtils.ExecuteBooleanOperation(solidA, solidB, booleanOperationsType);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result = BooleanOperationsUtils.ExecuteBooleanOperation(solidB, solidA, booleanOperationsType);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 计算几何体的中点
|
||||
/// </summary>
|
||||
/// <param name="solid"></param>
|
||||
/// <returns></returns>
|
||||
public static XYZ GetSolidCenter(this Solid solid)
|
||||
{
|
||||
XYZ centerPoint = null;
|
||||
foreach (Face geoFace in solid.Faces)
|
||||
{
|
||||
PlanarFace temFace = geoFace as PlanarFace;
|
||||
if (Math.Abs(temFace.FaceNormal.X) < 0.01 && Math.Abs(temFace.FaceNormal.Y) < 0.01 && temFace.FaceNormal.Z < 0)
|
||||
{
|
||||
var faceBound = temFace.GetBoundingBox();
|
||||
UV maxUV = faceBound.Max;
|
||||
UV minUV = faceBound.Min;
|
||||
UV uvPoint = (maxUV + minUV) / 2;
|
||||
centerPoint = temFace.Evaluate(uvPoint).SetZ();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return centerPoint;
|
||||
}
|
||||
// <summary>
|
||||
/// 传入集合的集合体,批量操作
|
||||
/// </summary>
|
||||
/// <param name="solids"></param>
|
||||
/// <param name="booleanOperationsType"></param>
|
||||
/// <returns></returns>
|
||||
public static Solid SolidBatchBoolean(this List<Solid> solids, BooleanOperationsType booleanOperationsType)
|
||||
{
|
||||
Solid firstSolid = solids[0];
|
||||
solids.RemoveAt(0);
|
||||
//对所有的几何体进行融合
|
||||
foreach (var oneSoild in solids)
|
||||
{
|
||||
try
|
||||
{
|
||||
firstSolid = GeoMethod.SolidBooleanOperation(firstSolid, oneSoild, booleanOperationsType);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return firstSolid;
|
||||
}
|
||||
}
|
||||
}
|
299
20210129/zzx-20210129作业/Method/LineListMethod.cs
Normal file
299
20210129/zzx-20210129作业/Method/LineListMethod.cs
Normal file
@ -0,0 +1,299 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
public static class LineListMethod
|
||||
{
|
||||
/// <summary>
|
||||
/// 封闭轮廓线首尾相连
|
||||
/// </summary>
|
||||
/// <param name="lineList"></param>
|
||||
/// <returns></returns>
|
||||
public static List<Line> OrderLineList(this List<Line> lineList)
|
||||
{
|
||||
List<Autodesk.Revit.DB.Line> lines = new List<Autodesk.Revit.DB.Line> { lineList[0] };
|
||||
lineList.RemoveAt(0);
|
||||
XYZ firstStart = lines[0].GetEndPoint(0);
|
||||
while (lineList.Count != 0)
|
||||
{
|
||||
XYZ lastEnd = lines.Last().GetEndPoint(1);
|
||||
//回到起点
|
||||
if (lastEnd.DistanceTo(firstStart) < 1 / 304.8)
|
||||
{
|
||||
break;
|
||||
}
|
||||
int index = lineList.FindIndex(m => m.GetEndPoint(0).DistanceTo(lastEnd) < 1 / 304.8);
|
||||
if (index != -1)//最后一根线的终点与集合中某条线的起点相同
|
||||
{
|
||||
lines.Add(lineList.ElementAt(index));
|
||||
//移除线
|
||||
lineList.RemoveAt(index);
|
||||
}
|
||||
else//最后一根线的终点与集合中某条线的终点相同
|
||||
{
|
||||
index = lineList.FindIndex(m => m.GetEndPoint(1).DistanceTo(lastEnd) < 1 / 304.8);
|
||||
if (index != -1)//如果存在就将线前后翻转一下,添加进集合
|
||||
{
|
||||
lines.Add(lineList.ElementAt(index).CreateReversed() as Autodesk.Revit.DB.Line);
|
||||
//移除线
|
||||
lineList.RemoveAt(index);
|
||||
}
|
||||
else//可能是没有线与它相同
|
||||
{
|
||||
//可能是不封闭的
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
//MessageBox.Show(lines.Count.ToString());
|
||||
CurveLoop cuLoop = CurveLoop.Create(lines.OfType<Curve>().ToList());
|
||||
if (!cuLoop.IsOpen())
|
||||
return lines;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 桥接线
|
||||
/// </summary>
|
||||
/// <param name="lines"></param>
|
||||
/// <param name="range"></param>
|
||||
/// <returns></returns>
|
||||
public static List<Autodesk.Revit.DB.Line> JoinLines(this List<Autodesk.Revit.DB.Line> lines, double range)
|
||||
{
|
||||
var newLines = new List<Autodesk.Revit.DB.Line>();
|
||||
//先按是否平行且间距小于range分组
|
||||
List<List<Autodesk.Revit.DB.Line>> lineListList = new List<List<Autodesk.Revit.DB.Line>>();
|
||||
for (int i = 0; i < lines.Count; i++)
|
||||
{
|
||||
int index = -1;
|
||||
for (int j = 0; j < lineListList.Count; j++)
|
||||
{
|
||||
XYZ midPoint = lineListList[j][0].Evaluate(0.5, true);
|
||||
Autodesk.Revit.DB.Line l = Autodesk.Revit.DB.Line.CreateBound(midPoint + 1000 * lineListList[j][0].Direction, midPoint - 1000 * lineListList[j][0].Direction);
|
||||
XYZ pj = l.Project(lines[i].GetEndPoint(0)).XYZPoint;
|
||||
double d = pj.DistanceTo(lines[i].GetEndPoint(0));
|
||||
if (lineListList[j][0].Direction.IsParallel(lines[i].Direction) && d < range)
|
||||
{
|
||||
index = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index == -1) lineListList.Add(new List<Autodesk.Revit.DB.Line>() { lines[i] });
|
||||
else lineListList[index].Add(lines[i]);
|
||||
}
|
||||
for (int i = 0; i < lineListList.Count; i++)
|
||||
{
|
||||
List<Autodesk.Revit.DB.Line> lineList = lineListList[i];
|
||||
//方向
|
||||
var dirt = lineList.ElementAt(0).Direction;
|
||||
//点乘信息 线索引 端点坐标 坐标点乘方向
|
||||
List<Tuple<int, XYZ, double>> lineEndInfoList = new List<Tuple<int, XYZ, double>>();
|
||||
//点乘信息添加
|
||||
for (int j = 0; j < lineList.Count; j++)
|
||||
{
|
||||
lineEndInfoList.Add(Tuple.Create(j, lineList[j].GetEndPoint(0), lineList[j].GetEndPoint(0).DotProduct(dirt).Round(2)));
|
||||
lineEndInfoList.Add(Tuple.Create(j, lineList[j].GetEndPoint(1), lineList[j].GetEndPoint(1).DotProduct(dirt).Round(2)));
|
||||
}
|
||||
//排序
|
||||
lineEndInfoList = lineEndInfoList.OrderBy(x => x.Item3).ToList();
|
||||
List<XYZ> xyzList = new List<XYZ>();
|
||||
|
||||
while (lineEndInfoList.Count > 0)
|
||||
{
|
||||
var first = lineEndInfoList.ElementAt(0);
|
||||
lineEndInfoList.RemoveAt(0);
|
||||
xyzList.Add(first.Item2);
|
||||
//拿到第一个线对应的另外一个点
|
||||
int index = lineEndInfoList.FindIndex(x => x.Item1 == first.Item1);
|
||||
while (true)
|
||||
{
|
||||
//拿到这部分的集合
|
||||
var tempInfoList = lineEndInfoList.GetRange(0, index + 1);
|
||||
|
||||
//最后的一个元素
|
||||
var endTemp = tempInfoList.Last();
|
||||
|
||||
//移除该集合
|
||||
lineEndInfoList.RemoveRange(0, index + 1);
|
||||
|
||||
//拿到只有一个点在该区域的集合
|
||||
List<Tuple<int, XYZ, double>> remainTempList = new List<Tuple<int, XYZ, double>>();
|
||||
//排除掉中间有2个索引的线
|
||||
while (tempInfoList.Count > 0)
|
||||
{
|
||||
var firstTemp = tempInfoList.ElementAt(0);
|
||||
tempInfoList.RemoveAt(0);
|
||||
var tempIndex = tempInfoList.FindIndex(x => x.Item1 == firstTemp.Item1);
|
||||
if (tempIndex != -1)
|
||||
{
|
||||
tempInfoList.RemoveAt(tempIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
remainTempList.Add(firstTemp);
|
||||
}
|
||||
}
|
||||
if (remainTempList.Count == 0)
|
||||
{
|
||||
xyzList.Add(endTemp.Item2);
|
||||
break;
|
||||
}
|
||||
//找对应最后的一个点的信息
|
||||
index = remainTempList.Select(x => lineEndInfoList.FindIndex(m => m.Item1 == x.Item1)).OrderBy(x => x).Last();
|
||||
if (index == -1)
|
||||
{
|
||||
if (lineEndInfoList.Count != 0 && lineEndInfoList[0].Item2.DistanceTo(endTemp.Item2) < 1 / 304.8)
|
||||
{
|
||||
//两线桥接--只有一个交点的情况
|
||||
var firstIndex = lineEndInfoList[0].Item1;
|
||||
lineEndInfoList.RemoveAt(0);
|
||||
index = lineEndInfoList.FindIndex(x => x.Item1 == firstIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
xyzList.Add(endTemp.Item2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int k = 0; k < xyzList.Count; k += 2)
|
||||
{
|
||||
XYZ pStart = xyzList[k];
|
||||
XYZ pEnd = xyzList[k + 1];
|
||||
if (pStart.DistanceTo(pEnd) > 1 / 304.8)
|
||||
{
|
||||
//var mc = Line.CreateBound(pStart, pEnd).GenerateMc(doc, true);
|
||||
//mc.SetData(Properties.Resources.TestGuid, Properties.Resources.TestName, i.ToString() + " " + lineListList[i].Count);
|
||||
newLines.Add(Autodesk.Revit.DB.Line.CreateBound(pStart, pEnd));
|
||||
}
|
||||
}
|
||||
}
|
||||
return newLines;
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 线相交打断
|
||||
/// </summary>
|
||||
/// <param name="curveList"></param>
|
||||
/// <returns></returns>
|
||||
public static List<Line> BreakInterLine(this List<Line> curveList)
|
||||
{
|
||||
List<Line> lineList = new List<Line>();
|
||||
foreach (var m in curveList)
|
||||
{
|
||||
List<Line> breakLineList = new List<Line>();
|
||||
int index = curveList.IndexOf(m);
|
||||
//存储所需要的交点
|
||||
List<XYZ> intersectPointList = new List<XYZ>();
|
||||
//复制m线段然后变成无限长
|
||||
Line mLine = Line.CreateBound(m.GetEndPoint(0), m.GetEndPoint(1));
|
||||
mLine.MakeUnbound();
|
||||
|
||||
for (int i = 0; i < curveList.Count; i++)
|
||||
{
|
||||
if (mLine.Direction.IsParallel(curveList[i].Direction)) continue;
|
||||
Line iLine = Line.CreateBound(curveList[i].GetEndPoint(0), curveList[i].GetEndPoint(1));
|
||||
iLine.MakeUnbound();
|
||||
mLine.Intersect(iLine, out IntersectionResultArray resultArray);
|
||||
if (resultArray?.get_Item(0)?.XYZPoint != null)
|
||||
{
|
||||
XYZ resultPoint = resultArray.get_Item(0).XYZPoint;
|
||||
double iDistance = curveList[i].GetEndPoint(0).DistanceTo(resultPoint) + curveList[i].GetEndPoint(1).DistanceTo(resultPoint);
|
||||
double mDistance = m.GetEndPoint(0).DistanceTo(resultPoint) + m.GetEndPoint(1).DistanceTo(resultPoint);
|
||||
if (iDistance - curveList[i].Length < 1 / 304.8 && mDistance - m.Length < 1 / 304.8)
|
||||
{
|
||||
intersectPointList.Add(resultArray.get_Item(0).XYZPoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
//ToDo:不太确定起终点不放进去会不会有问题
|
||||
//点去重
|
||||
intersectPointList = intersectPointList.Where((x, i) => intersectPointList.FindIndex(c => c.DistanceTo(x) < 1 / 304.8) == i).ToList();
|
||||
//排列点并重新生成线段集合
|
||||
intersectPointList = intersectPointList.OrderBy(x => m.GetEndPoint(0).DistanceTo(x)).ToList();
|
||||
//线段集合
|
||||
List<Line> lines = new List<Line>();
|
||||
for (int i = 0; i < intersectPointList.Count - 1; i++)
|
||||
{
|
||||
Line l = Line.CreateBound(intersectPointList[i], intersectPointList[i + 1]);
|
||||
lines.Add(l);
|
||||
}
|
||||
lineList.AddRange(lines);
|
||||
|
||||
}
|
||||
return lineList;
|
||||
}
|
||||
/// <summary>
|
||||
/// 去除孤线
|
||||
/// </summary>
|
||||
/// <param name="curveList"></param>
|
||||
/// <returns></returns>
|
||||
public static List<Line> DeleteSolitary(this List<Line> curveList)
|
||||
{
|
||||
List<Line> lineList = new List<Line>();
|
||||
foreach (var m in curveList)
|
||||
{
|
||||
List<Line> breakLineList = new List<Line>();
|
||||
int index = curveList.IndexOf(m);
|
||||
//存储所需要的交点
|
||||
List<XYZ> intersectPointList = new List<XYZ>();
|
||||
//复制m线段然后变成无限长
|
||||
Line mLine = Line.CreateBound(m.GetEndPoint(0), m.GetEndPoint(1));
|
||||
mLine.MakeUnbound();
|
||||
|
||||
for (int i = 0; i < curveList.Count; i++)
|
||||
{
|
||||
if (mLine.Direction.IsParallel(curveList[i].Direction)) continue;
|
||||
Line iLine = Line.CreateBound(curveList[i].GetEndPoint(0), curveList[i].GetEndPoint(1));
|
||||
iLine.MakeUnbound();
|
||||
mLine.Intersect(iLine, out IntersectionResultArray resultArray);
|
||||
if (resultArray?.get_Item(0)?.XYZPoint != null)
|
||||
{
|
||||
XYZ resultPoint = resultArray.get_Item(0).XYZPoint;
|
||||
double iDistance = curveList[i].GetEndPoint(0).DistanceTo(resultPoint) + curveList[i].GetEndPoint(1).DistanceTo(resultPoint);
|
||||
double mDistance = m.GetEndPoint(0).DistanceTo(resultPoint) + m.GetEndPoint(1).DistanceTo(resultPoint);
|
||||
if (iDistance - curveList[i].Length < 1 / 304.8 && mDistance - m.Length < 1 / 304.8)
|
||||
{
|
||||
intersectPointList.Add(resultArray.get_Item(0).XYZPoint);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
//点去重
|
||||
intersectPointList = intersectPointList.Where((x, i) => intersectPointList.FindIndex(c => c.DistanceTo(x) < 1 / 304.8) == i).ToList();
|
||||
//排列点并重新生成线段集合
|
||||
intersectPointList = intersectPointList.OrderBy(x => m.GetEndPoint(0).DistanceTo(x)).ToList();
|
||||
//线段集合
|
||||
List<Line> lines = new List<Line>();
|
||||
for (int i = 0; i < intersectPointList.Count - 1; i++)
|
||||
{
|
||||
Line l = Line.CreateBound(intersectPointList[i], intersectPointList[i + 1]);
|
||||
lines.Add(l);
|
||||
}
|
||||
lineList.AddRange(lines);
|
||||
|
||||
}
|
||||
return lineList;
|
||||
}
|
||||
/// <summary>
|
||||
/// 生成详图线,事务在方法外面开启
|
||||
/// </summary>
|
||||
/// <param name="lines"></param>
|
||||
/// <param name="v"></param>
|
||||
public static void CreateDetailLine(this List<Line> lines, Document doc, View v)
|
||||
{
|
||||
foreach (var l in lines)
|
||||
{
|
||||
doc.Create.NewDetailCurve(v, l);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
138
20210129/zzx-20210129作业/Method/LineMethod.cs
Normal file
138
20210129/zzx-20210129作业/Method/LineMethod.cs
Normal file
@ -0,0 +1,138 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
public static class LineMethod
|
||||
{
|
||||
/// <summary>
|
||||
/// 判断两条线是否相交
|
||||
/// </summary>
|
||||
/// <param name="l1"></param>
|
||||
/// <param name="l2"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsIntersect(this Line l1, Line l2)
|
||||
{
|
||||
bool result = false;
|
||||
Line l1LengthenLine = Line.CreateBound(l1.GetEndPoint(0), l1.GetEndPoint(1));
|
||||
l1LengthenLine.MakeUnbound();
|
||||
var proResult = l1LengthenLine.Project(l2.Evaluate(0.5, true));
|
||||
if (proResult.XYZPoint.IsOnLine(l1) && proResult.XYZPoint.IsOnLine(l2))
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 是否共线
|
||||
/// </summary>
|
||||
/// <param name="line1"></param>
|
||||
/// <param name="line2"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsCollinear(this Autodesk.Revit.DB.Line line1, Autodesk.Revit.DB.Line line2, double distance = 0.05)
|
||||
{
|
||||
return line1.Direction.IsParallel(line2.Direction) && line2.DisToPoint(line1.Evaluate(0.5, true), out XYZ direction) < distance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否是同一根线
|
||||
/// </summary>
|
||||
/// <param name="l1"></param>
|
||||
/// <param name="l2"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsSameLine(this Autodesk.Revit.DB.Line l1, Autodesk.Revit.DB.Line l2)
|
||||
{
|
||||
if (l1.GetEndPoint(0).DistanceTo(l2.GetEndPoint(0)) < 1 / 304.8 && l1.GetEndPoint(1).DistanceTo(l2.GetEndPoint(1)) < 1 / 304.8)
|
||||
return true;
|
||||
else if (l1.GetEndPoint(1).DistanceTo(l2.GetEndPoint(0)) < 1 / 304.8 && l1.GetEndPoint(0).DistanceTo(l2.GetEndPoint(1)) < 1 / 304.8)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 设置z轴参数
|
||||
/// </summary>
|
||||
/// <param name="line"></param>
|
||||
/// <param name="z"></param>
|
||||
/// <returns></returns>
|
||||
public static Autodesk.Revit.DB.Line SetZ(this Autodesk.Revit.DB.Line line, double z = 0)
|
||||
{
|
||||
if (line.Direction.IsParallel(XYZ.BasisZ))
|
||||
throw new ArgumentException("线无法进行Z值设置");
|
||||
if (line.GetEndPoint(0).SetZ(z).DistanceTo(line.GetEndPoint(1).SetZ(z)) < 1 / 304.8)
|
||||
throw new ArgumentException("线太短");
|
||||
return Autodesk.Revit.DB.Line.CreateBound(line.GetEndPoint(0).SetZ(z), line.GetEndPoint(1).SetZ(z));
|
||||
}
|
||||
|
||||
public static Curve SetZ(this Curve curve, double z = 0)
|
||||
{
|
||||
var line = curve as Autodesk.Revit.DB.Line;
|
||||
if (line.Direction.IsParallel(XYZ.BasisZ))
|
||||
throw new ArgumentException("线无法进行Z值设置");
|
||||
if (line.GetEndPoint(0).SetZ(z).DistanceTo(line.GetEndPoint(1).SetZ(z)) < 1 / 304.8)
|
||||
throw new ArgumentException("线太短");
|
||||
return Autodesk.Revit.DB.Line.CreateBound(line.GetEndPoint(0).SetZ(z), line.GetEndPoint(1).SetZ(z));
|
||||
}
|
||||
/// <summary>
|
||||
/// 点到线的距离
|
||||
/// </summary>
|
||||
/// <param name="line"></param>
|
||||
/// <param name="point"></param>
|
||||
/// <param name="direction"></param>
|
||||
/// <returns></returns>
|
||||
public static double DisToPoint(this Autodesk.Revit.DB.Line line, XYZ point, out XYZ direction)
|
||||
{
|
||||
var nwLine = line.SetZ();
|
||||
nwLine.MakeUnbound();
|
||||
XYZ projectPoint = nwLine.Project(point.SetZ()).XYZPoint;
|
||||
double distance = point.SetZ().DistanceTo(projectPoint);
|
||||
direction = (point.SetZ() - projectPoint).Normalize();
|
||||
return distance;
|
||||
|
||||
}
|
||||
public static double DisToPoint(this Autodesk.Revit.DB.Line line, XYZ point)
|
||||
{
|
||||
var nwLine = line.SetZ();
|
||||
nwLine.MakeUnbound();
|
||||
XYZ projectPoint = nwLine.Project(point.SetZ()).XYZPoint;
|
||||
double distance = point.SetZ().DistanceTo(projectPoint);
|
||||
return distance;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 两平行线求中线(中线长度按最长条)
|
||||
/// </summary>
|
||||
/// <param name="curve1"></param>
|
||||
/// <param name="curve2"></param>
|
||||
/// <param name="distance"></param>
|
||||
/// <returns></returns>
|
||||
public static Line GetMidLine(this Line curve1, Line curve2, out double distance)
|
||||
{
|
||||
distance = double.MaxValue;
|
||||
if (curve1.Length < curve2.Length)
|
||||
{
|
||||
var tempLine = curve1;
|
||||
curve1 = curve2;
|
||||
curve2 = tempLine;
|
||||
}
|
||||
Line midline;
|
||||
XYZ startPoint = curve1.GetEndPoint(0);
|
||||
XYZ endPoint = curve1.GetEndPoint(1);
|
||||
Line line2 = Line.CreateBound(curve2.GetEndPoint(0), curve2.GetEndPoint(1));
|
||||
line2.MakeUnbound();
|
||||
midline = Line.CreateBound((startPoint + line2.Project(startPoint).XYZPoint) / 2, (endPoint + line2.Project(endPoint).XYZPoint) / 2);
|
||||
distance = startPoint.DistanceTo(line2.Project(startPoint).XYZPoint).ToMM();
|
||||
return midline;
|
||||
}
|
||||
}
|
||||
}
|
110
20210129/zzx-20210129作业/Method/TestMethod.cs
Normal file
110
20210129/zzx-20210129作业/Method/TestMethod.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
public static class TestMethod
|
||||
{
|
||||
/// <summary>
|
||||
/// 点测试
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="doc"></param>
|
||||
/// <param name="isInTransaction"></param>
|
||||
public static void XYZTest(this XYZ x, Document doc)
|
||||
{
|
||||
Autodesk.Revit.DB.Line l = Autodesk.Revit.DB.Line.CreateBound(x, new XYZ(x.X, x.Y + 100, x.Z));
|
||||
XYZ basicZ = XYZ.BasisZ;
|
||||
if (l.Direction.AngleTo(XYZ.BasisZ) < 0.0001 || l.Direction.AngleTo(-XYZ.BasisZ) < 0.0001)
|
||||
basicZ = XYZ.BasisY;
|
||||
XYZ normal = basicZ.CrossProduct(l.Direction).Normalize();
|
||||
Plane pl = Plane.CreateByNormalAndOrigin(normal, l.GetEndPoint(0));
|
||||
Transaction transCreate = null;
|
||||
if (!doc.IsModifiable)
|
||||
transCreate = new Transaction(doc, "模型线测试");
|
||||
transCreate?.Start();
|
||||
SketchPlane sktpl = SketchPlane.Create(doc, pl);
|
||||
ModelCurve mc = doc.Create.NewModelCurve(l, sktpl);
|
||||
transCreate?.Commit();
|
||||
}
|
||||
/// <summary>
|
||||
/// 测试线
|
||||
/// </summary>
|
||||
/// <param name="l"></param>
|
||||
/// <param name="doc"></param>
|
||||
/// <param name="isInTransaction"></param>
|
||||
public static ModelCurve LineTest(this Line l, Document doc)
|
||||
{
|
||||
XYZ basicZ = XYZ.BasisZ;
|
||||
if (l.Direction.AngleTo(XYZ.BasisZ) < 0.0001 || l.Direction.AngleTo(-XYZ.BasisZ) < 0.0001)
|
||||
basicZ = XYZ.BasisY;
|
||||
XYZ normal = basicZ.CrossProduct(l.Direction).Normalize();
|
||||
Plane pl = Plane.CreateByNormalAndOrigin(normal, l.GetEndPoint(0));
|
||||
Transaction transCreate = null;
|
||||
if (!doc.IsModifiable)
|
||||
transCreate = new Transaction(doc, "模型线测试");
|
||||
transCreate?.Start();
|
||||
SketchPlane sktpl = SketchPlane.Create(doc, pl);
|
||||
ModelCurve mc = doc.Create.NewModelCurve(l, sktpl);
|
||||
transCreate?.Commit();
|
||||
return mc;
|
||||
}
|
||||
public static ModelCurve ArcTest(this Arc arc, Document doc)
|
||||
{
|
||||
//16
|
||||
//Plane plane = new Plane(arc.Normal, arc.Center);
|
||||
//17-20
|
||||
Plane plane = Plane.CreateByNormalAndOrigin(arc.Normal, arc.Center);
|
||||
Transaction transCreate = null;
|
||||
if (!doc.IsModifiable)
|
||||
transCreate = new Transaction(doc, "模型线测试");
|
||||
transCreate?.Start();
|
||||
SketchPlane sktpl = SketchPlane.Create(doc, plane);
|
||||
ModelCurve mc = doc.Create.NewModelCurve(arc, sktpl);
|
||||
transCreate?.Commit();
|
||||
return mc;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 线集合测试
|
||||
/// </summary>
|
||||
/// <param name="lines"></param>
|
||||
/// <param name="doc"></param>
|
||||
/// <returns></returns>
|
||||
public static List<ModelCurve> lineListText(this List<Line> lines, Document doc)
|
||||
{
|
||||
List<ModelCurve> curves = new List<ModelCurve>();
|
||||
foreach (var l in lines)
|
||||
{
|
||||
ModelCurve mc = l.LineTest(doc);
|
||||
curves.Add(mc);
|
||||
}
|
||||
return curves;
|
||||
}
|
||||
/// <summary>
|
||||
/// 几何体测试
|
||||
/// </summary>
|
||||
/// <param name="solid"></param>
|
||||
/// <param name="doc"></param>
|
||||
/// <param name="b"></param>
|
||||
public static void SolidText(this Solid solid, Document doc)
|
||||
{
|
||||
Transaction transCreate = null;
|
||||
if (!doc.IsModifiable)
|
||||
transCreate = new Transaction(doc, "模型线测试");
|
||||
transCreate?.Start();
|
||||
DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_Mass));
|
||||
if (ds != null)
|
||||
{
|
||||
ds.AppendShape(new List<GeometryObject>() { solid });
|
||||
}
|
||||
transCreate?.Commit();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
37
20210129/zzx-20210129作业/Method/VectorsMethod.cs
Normal file
37
20210129/zzx-20210129作业/Method/VectorsMethod.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
public static class VectorsMethod
|
||||
{
|
||||
/// <summary>
|
||||
/// 向量是否平行
|
||||
/// </summary>
|
||||
/// <param name="vector1"></param>
|
||||
/// <param name="vector2"></param>
|
||||
/// <param name="v">true为同向平行,false为反向平行,null为平行</param>
|
||||
/// <param name="tolerance">允许误差的角度</param>
|
||||
/// <returns></returns>
|
||||
public static bool IsParallel(this XYZ vector1, XYZ vector2, bool? v = null, double tolerance = 0.1)
|
||||
{
|
||||
if (v == null)
|
||||
{
|
||||
return vector1.AngleTo(vector2) > (180 - tolerance).ToRad() || vector1.DistanceTo(vector2) < tolerance.ToRad();
|
||||
}
|
||||
else if (v == true)
|
||||
{
|
||||
return vector1.AngleTo(vector2) < tolerance.ToRad();
|
||||
}
|
||||
else
|
||||
{
|
||||
return vector1.AngleTo(vector2) > (180 - tolerance).ToRad();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
67
20210129/zzx-20210129作业/Method/ViewMethod.cs
Normal file
67
20210129/zzx-20210129作业/Method/ViewMethod.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
public static class ViewMethod
|
||||
{
|
||||
/// <summary>
|
||||
/// 新建面积方案,事务在方法外开启
|
||||
/// </summary>
|
||||
/// <param name="doc"></param>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
public static AreaScheme CreateAreaView(Document doc, string name)
|
||||
{
|
||||
AreaScheme areaScheme = new FilteredElementCollector(doc).OfClass(typeof(AreaScheme)).Cast<AreaScheme>().FirstOrDefault();
|
||||
|
||||
ICollection<ElementId> newElemIds = ElementTransformUtils.CopyElement(doc, areaScheme.Id, XYZ.Zero);
|
||||
AreaScheme newVft = doc.GetElement(newElemIds.First()) as AreaScheme;
|
||||
newVft.Name = name;
|
||||
return newVft;
|
||||
}
|
||||
/// <summary>
|
||||
/// 视图浏览器设置,(事务在方法外面开启)
|
||||
/// </summary>
|
||||
/// <param name="v"></param>
|
||||
/// <param name="doc"></param>
|
||||
/// <param name="name1"></param>
|
||||
/// <param name="name2"></param>
|
||||
public static void SetView(this View v, Document doc, string name1, string name2)
|
||||
{
|
||||
BrowserOrganization hh = BrowserOrganization.GetCurrentBrowserOrganizationForViews(doc);
|
||||
var aa = hh.GetFolderItems(doc.ActiveView.Id);
|
||||
List<string> viewNameList = new List<string>();
|
||||
foreach (var item in aa)
|
||||
{
|
||||
if (item.ElementId.IntegerValue > 0)
|
||||
{
|
||||
viewNameList.Add((doc.GetElement(item.ElementId) as ParameterElement).GetDefinition().Name);
|
||||
}
|
||||
}
|
||||
if (viewNameList.Count != 0)
|
||||
{
|
||||
v.GetParameters(viewNameList[0]).First().Set(name1);
|
||||
if (viewNameList.Count > 1)
|
||||
{
|
||||
v.GetParameters(viewNameList[1]).First().Set(name2);
|
||||
if (viewNameList.Count > 2)
|
||||
{
|
||||
viewNameList.RemoveAt(0);
|
||||
viewNameList.RemoveAt(1);
|
||||
for (int i = 0; i < viewNameList.Count; i++)
|
||||
{
|
||||
v.GetParameters(viewNameList[i]).First().SetValueString(name2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
82
20210129/zzx-20210129作业/Method/WallMethod.cs
Normal file
82
20210129/zzx-20210129作业/Method/WallMethod.cs
Normal file
@ -0,0 +1,82 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
public static class WallMethod
|
||||
{
|
||||
/// <summary>
|
||||
/// 找到相交的墙
|
||||
/// </summary>
|
||||
/// <param name="wall"></param>
|
||||
/// <returns></returns>
|
||||
public static List<ElementId> GetGeoInsterEle(this Wall wall)
|
||||
{
|
||||
List<ElementId> elemIdList = new List<ElementId>();
|
||||
var geo = wall.get_Geometry(new Options());
|
||||
foreach (GeometryObject geoObject in geo)
|
||||
{
|
||||
Solid geoSolid = geoObject as Solid;
|
||||
foreach (Face geoFace in geoSolid.Faces)
|
||||
{
|
||||
var generatingEleId = wall.GetGeneratingElementIds(geoFace).Where(x => !(elemIdList.Contains(x))).ToList();
|
||||
generatingEleId.Remove(wall.Id);
|
||||
elemIdList.AddRange(generatingEleId);
|
||||
}
|
||||
}
|
||||
return elemIdList;
|
||||
}
|
||||
/// <summary>
|
||||
/// 判断墙是否相连
|
||||
/// </summary>
|
||||
/// <param name="judgeWall"></param>
|
||||
/// <param name="proWall"></param>
|
||||
/// <param name="needPoint"></param>
|
||||
/// <returns></returns>
|
||||
public static bool WallIsConnectedWall(this Wall judgeWall, Wall proWall, out List<XYZ> needPoint)
|
||||
{
|
||||
bool result = false;
|
||||
needPoint = new List<XYZ>();
|
||||
Line judgeLocationLine = ((judgeWall.Location as LocationCurve).Curve as Line).SetZ();
|
||||
Line proLocationLine = ((proWall.Location as LocationCurve).Curve as Line).SetZ();
|
||||
//只对平行的处理
|
||||
if (!judgeLocationLine.Direction.IsParallel(proLocationLine.Direction)) return result;
|
||||
var judgeGeo = judgeWall.get_Geometry(new Options());
|
||||
Face jugeDownFace = GeoMethod.GetGeoEleDownFace(judgeGeo);
|
||||
var proGeo = proWall.get_Geometry(new Options());
|
||||
Face proDownFace = GeoMethod.GetGeoEleDownFace(proGeo);
|
||||
List<Solid> solidList = new List<Solid>();
|
||||
//想法一 用布尔运算去测试
|
||||
//收集几何体
|
||||
double judgeSolidSurfaceArea = 0;
|
||||
double prosolidSurfaceArea = 0;
|
||||
List<Solid> judgeSolids = GeoMethod.GetSolids(judgeGeo);
|
||||
List<Solid> proSolids = GeoMethod.GetSolids(proGeo);
|
||||
foreach (var judgeSolid in judgeSolids)
|
||||
{
|
||||
judgeSolidSurfaceArea += judgeSolid.SurfaceArea;
|
||||
solidList.Add(judgeSolid);
|
||||
}
|
||||
foreach (var proSolid in proSolids)
|
||||
{
|
||||
prosolidSurfaceArea += proSolid.SurfaceArea;
|
||||
solidList.Add(proSolid);
|
||||
}
|
||||
Solid fuseSolid = solidList.SolidBatchBoolean(BooleanOperationsType.Union);
|
||||
if (judgeSolidSurfaceArea + prosolidSurfaceArea - fuseSolid.SurfaceArea > 0)
|
||||
{
|
||||
result = true;
|
||||
//收集中间的两个点
|
||||
List<XYZ> pointList = new List<XYZ>() { judgeLocationLine.GetEndPoint(0), judgeLocationLine.GetEndPoint(1), proLocationLine.GetEndPoint(0), proLocationLine.GetEndPoint(1) };
|
||||
pointList = pointList.OrderBy(x => x.DotProduct(judgeLocationLine.Direction).Round(3)).ToList();
|
||||
needPoint.Add(pointList[1]);
|
||||
needPoint.Add(pointList[2]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
24
20210129/zzx-20210129作业/Method/WindowMethod.cs
Normal file
24
20210129/zzx-20210129作业/Method/WindowMethod.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
public static class WindowMethod
|
||||
{
|
||||
/// <summary>
|
||||
/// 设置窗体靠左上角
|
||||
/// </summary>
|
||||
/// <param name="win"></param>
|
||||
/// <param name="ActiveUIDocument"></param>
|
||||
//public static void SetLeft(this Window win, Autodesk.Revit.UI.UIDocument ActiveUIDocument)
|
||||
//{
|
||||
// Autodesk.Revit.UI.UIView uView = ActiveUIDocument.GetOpenUIViews().FirstOrDefault(x => x.ViewId == ActiveUIDocument.ActiveView.Id);
|
||||
// var retangle = uView.GetWindowRectangle();
|
||||
// win.Left = retangle.Left;
|
||||
// win.Top = retangle.Top;
|
||||
//}
|
||||
}
|
||||
}
|
234
20210129/zzx-20210129作业/Method/XYZMethod.cs
Normal file
234
20210129/zzx-20210129作业/Method/XYZMethod.cs
Normal file
@ -0,0 +1,234 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
public static class XYZMethod
|
||||
{
|
||||
/// <summary>
|
||||
/// 坐标点去重
|
||||
/// </summary>
|
||||
/// <param name="xyzs"></param>
|
||||
/// <returns></returns>
|
||||
public static List<XYZ> DeWeighting(this List<XYZ> xyzs)
|
||||
{
|
||||
for (int i = 0; i < xyzs.Count; i++)
|
||||
{
|
||||
for (int j = 0; j < xyzs.Count; j++)
|
||||
{
|
||||
if (i == j) continue;
|
||||
if (Comparison(xyzs[i], xyzs[j], 4, false))
|
||||
{
|
||||
xyzs.RemoveAt(j);
|
||||
j--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return xyzs;
|
||||
}
|
||||
public static bool Comparison(XYZ xyz1, XYZ xyz2, int v, bool is3d)
|
||||
{
|
||||
double x1 = xyz1.X;
|
||||
double y1 = xyz1.Y;
|
||||
double x2 = xyz2.X;
|
||||
double y2 = xyz2.Y;
|
||||
if (is3d)
|
||||
{
|
||||
double z1 = xyz1.Z;
|
||||
double z2 = xyz2.Z;
|
||||
return Math.Round(x1, v) == Math.Round(x2, v) && Math.Round(y1, v) == Math.Round(y2, v) && Math.Round(z1, v) == Math.Round(z2, v);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Math.Round(x1, v) == Math.Round(x2, v) && Math.Round(y1, v) == Math.Round(y2, v);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 排序
|
||||
/// ps:顺时针或者逆时针,点集合中找到最大的轮廓
|
||||
/// </summary>
|
||||
/// <param name="points"></param>
|
||||
public static void ClockwiseSortPoints(List<XYZ> points)
|
||||
{
|
||||
//计算重心
|
||||
XYZ center = new XYZ();
|
||||
double X = 0, Y = 0;
|
||||
for (int i = 0; i < points.Count; i++)
|
||||
{
|
||||
X += points[i].X;
|
||||
Y += points[i].Y;
|
||||
}
|
||||
|
||||
double centerX = (int)X / points.Count;
|
||||
double centerY = (int)Y / points.Count;
|
||||
center = new XYZ(centerX, centerY, 0);
|
||||
//冒泡排序
|
||||
for (int i = 0; i < points.Count - 1; i++)
|
||||
{
|
||||
for (int j = 0; j < points.Count - i - 1; j++)
|
||||
{
|
||||
if (PointCmp(points[j], points[j + 1], center))
|
||||
{
|
||||
XYZ tmp = points[j];
|
||||
points[j] = points[j + 1];
|
||||
points[j + 1] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断两点绕中心点的顺逆时针方向
|
||||
/// 若点a大于点b,即点a在点b顺时针方向,返回true,否则返回false
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <param name="center"></param>
|
||||
public static bool PointCmp(XYZ a, XYZ b, XYZ center)
|
||||
{
|
||||
if (a.X >= 0 && b.X < 0) return true;
|
||||
if (a.X == 0 && b.X == 0) return a.Y > b.Y;
|
||||
//向量OA和向量OB的叉积
|
||||
int det = Convert.ToInt32((a.X - center.X) * (b.Y - center.Y) - (b.X - center.X) * (a.Y - center.Y));
|
||||
if (det < 0) return true;
|
||||
if (det > 0) return false;
|
||||
//向量OA和向量OB共线,以距离判断大小
|
||||
double d1 = (a.X - center.X) * (a.X - center.X) + (a.Y - center.Y) * (a.Y - center.Y);
|
||||
double d2 = (b.X - center.X) * (b.X - center.Y) + (b.Y - center.Y) * (b.Y - center.Y);
|
||||
return d1 > d2;
|
||||
}
|
||||
|
||||
//判断点是否在线上
|
||||
public static bool IsOnLine(this XYZ xYZ, Line l)
|
||||
{
|
||||
bool result = false;
|
||||
XYZ start = l.GetEndPoint(0);
|
||||
XYZ end = l.GetEndPoint(1);
|
||||
double startDistance = xYZ.SetZ().DistanceTo(start.SetZ());
|
||||
double endDistance = xYZ.SetZ().DistanceTo(end.SetZ());
|
||||
if (startDistance + endDistance < l.Length + 5.ToFeet())
|
||||
{
|
||||
result = true;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断点是否在轮廓内
|
||||
/// </summary>
|
||||
/// <param name="TargetPoint"></param>
|
||||
/// <param name="xYZ">目标延长点</param>
|
||||
/// <param name="lines"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsInsideOutline(this XYZ TargetPoint, XYZ xYZ, List<Line> lines)
|
||||
{
|
||||
bool result = true;
|
||||
int insertCount = 0;
|
||||
Line rayLine = Line.CreateBound(TargetPoint, xYZ).SetZ(0);
|
||||
foreach (var areaLine in lines)
|
||||
{
|
||||
var interResult = areaLine.SetZ().Intersect(rayLine, out IntersectionResultArray resultArray);
|
||||
var insPoint = resultArray?.get_Item(0);
|
||||
if (insPoint != null)
|
||||
{
|
||||
insertCount++;
|
||||
}
|
||||
}
|
||||
//如果次数为偶数就在外面,次数为奇数就在里面
|
||||
if (insertCount % 2 == 0)//偶数
|
||||
{
|
||||
return result = false;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 判断点是否在封闭轮廓内
|
||||
/// </summary>
|
||||
/// <param name="TargetPoint"></param>
|
||||
/// <param name="lines"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsInsideOutline(this XYZ TargetPoint, List<Line> lines)
|
||||
{
|
||||
bool result = true;
|
||||
int insertCount = 0;
|
||||
Line rayLine = Line.CreateBound(TargetPoint, TargetPoint.Add(XYZ.BasisX * 1000)).SetZ(0);
|
||||
foreach (var areaLine in lines)
|
||||
{
|
||||
var interResult = areaLine.SetZ().Intersect(rayLine, out IntersectionResultArray resultArray);
|
||||
var insPoint = resultArray?.get_Item(0);
|
||||
if (insPoint != null)
|
||||
{
|
||||
insertCount++;
|
||||
}
|
||||
}
|
||||
//如果次数为偶数就在外面,次数为奇数就在里面
|
||||
if (insertCount % 2 == 0)//偶数
|
||||
{
|
||||
return result = false;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 英尺转毫米
|
||||
/// </summary>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
public static double ToMM(this double b)
|
||||
{
|
||||
return UnitUtils.Convert(b, DisplayUnitType.DUT_DECIMAL_FEET, DisplayUnitType.DUT_MILLIMETERS);
|
||||
}
|
||||
/// <summary>
|
||||
/// 毫米转英尺
|
||||
/// </summary>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
public static double ToFeet<T>(this T b) where T : struct
|
||||
{
|
||||
double.TryParse(b.ToString(), out var d);
|
||||
return UnitUtils.Convert(d, DisplayUnitType.DUT_MILLIMETERS, DisplayUnitType.DUT_DECIMAL_FEET);
|
||||
}
|
||||
/// <summary>
|
||||
/// 平方英尺转平方米
|
||||
/// </summary>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
public static double ToSquareMeters(this double b)
|
||||
{
|
||||
return UnitUtils.Convert(b, DisplayUnitType.DUT_SQUARE_FEET, DisplayUnitType.DUT_SQUARE_METERS);
|
||||
}
|
||||
/// <summary>
|
||||
/// 平方米转平方英尺
|
||||
/// </summary>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
public static double ToSquareFeet(this double b)
|
||||
{
|
||||
return UnitUtils.Convert(b, DisplayUnitType.DUT_SQUARE_METERS, DisplayUnitType.DUT_SQUARE_FEET);
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置Z轴
|
||||
/// </summary>
|
||||
/// <param name="sPoint"></param>
|
||||
/// <param name="z"></param>
|
||||
/// <returns></returns>
|
||||
public static XYZ SetZ(this XYZ sPoint, double z = 0)
|
||||
{
|
||||
return new XYZ(sPoint.X, sPoint.Y, z);
|
||||
}
|
||||
/// <summary>
|
||||
/// 转弧度
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="t"></param>
|
||||
/// <returns></returns>
|
||||
public static double ToRad<T>(this T t) where T : struct
|
||||
{
|
||||
return double.Parse(t.ToString()) * Math.PI / 180;
|
||||
}
|
||||
}
|
||||
}
|
36
20210129/zzx-20210129作业/Properties/AssemblyInfo.cs
Normal file
36
20210129/zzx-20210129作业/Properties/AssemblyInfo.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的一般信息由以下
|
||||
// 控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("GraphicsStudy")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("GraphicsStudy")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2020")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
||||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
||||
//请将此类型的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("8b9832c5-1479-4c32-a7da-4b9b06880158")]
|
||||
|
||||
// 程序集的版本信息由下列四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
|
||||
//通过使用 "*",如下所示:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
67
20210129/zzx-20210129作业/json/ElementIdConverter.cs
Normal file
67
20210129/zzx-20210129作业/json/ElementIdConverter.cs
Normal file
@ -0,0 +1,67 @@
|
||||
/************************************************************************
|
||||
* ClassName : ElementIdConverter
|
||||
* Description :
|
||||
* Author : Einsam
|
||||
* CreateTime : 2019/4/1 17:40:38
|
||||
************************************************************************
|
||||
* █████▒█ ██ ▄████▄ ██ ▄█▀ ██████╗ ██╗ ██╗ ██████╗
|
||||
* ▓██ ▒ ██ ▓██▒▒██▀ ▀█ ██▄█▒ ██╔══██╗██║ ██║██╔════╝
|
||||
* ▒████ ░▓██ ▒██░▒▓█ ▄ ▓███▄░ ██████╔╝██║ ██║██║ ███╗
|
||||
* ░▓█▒ ░▓▓█ ░██░▒▓▓▄ ▄██▒▓██ █▄ ██╔══██╗██║ ██║██║ ██║
|
||||
* ░▒█░ ▒▒█████▓ ▒ ▓███▀ ░▒██▒ █▄ ██████╔╝╚██████╔╝╚██████╔╝
|
||||
* ▒ ░ ░▒▓▒ ▒ ▒ ░ ░▒ ▒ ░▒ ▒▒ ▓▒ ╚═════╝ ╚═════╝ ╚═════╝
|
||||
* ░ ░░▒░ ░ ░ ░ ▒ ░ ░▒ ▒░
|
||||
* ░ ░ ░░░ ░ ░ ░ ░ ░░ ░
|
||||
* ░ ░ ░ ░ ░
|
||||
* ░
|
||||
************************************************************************
|
||||
* Copyright @ u-BIM Dev. 2018 . All rights reserved.
|
||||
************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Autodesk.Revit.DB;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace GraphicsStudy.Json
|
||||
{
|
||||
public class ElementIdConverter : Newtonsoft.Json.JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNull();
|
||||
}
|
||||
else
|
||||
{
|
||||
JObject jObject = new JObject();
|
||||
jObject.Add("ID", (value as ElementId).IntegerValue);
|
||||
writer.WriteValue(jObject.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
var jObject = JObject.Parse(reader.Value as string);
|
||||
|
||||
int id = jObject.Value<int>("ID");
|
||||
|
||||
return new ElementId(id);
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
if (objectType == typeof(ElementId))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
46
20210129/zzx-20210129作业/json/JsonUtils.cs
Normal file
46
20210129/zzx-20210129作业/json/JsonUtils.cs
Normal file
@ -0,0 +1,46 @@
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy
|
||||
{
|
||||
public static class JsonUtils
|
||||
{
|
||||
private static readonly List<Newtonsoft.Json.JsonConverter> JsonConverters = new List<Newtonsoft.Json.JsonConverter>()
|
||||
{
|
||||
|
||||
new Json.XYZConverter(),
|
||||
new Json.LineConverter(),
|
||||
new Json.TransformConvert(),
|
||||
new Json.ElementIdConverter()
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// JSON序列化
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static string ObjectToJson(object obj)
|
||||
{
|
||||
JsonSerializerSettings jsSettings = new JsonSerializerSettings();
|
||||
jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
||||
return JsonConvert.SerializeObject(obj/*, JsonConverters.ToArray()*/);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// JSON反序列化
|
||||
/// </summary>
|
||||
/// <param name="jsonStr"></param>
|
||||
/// <returns></returns>
|
||||
public static T JsonToObject<T>(string jsonStr) where T : class
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(jsonStr/*, JsonConverters.ToArray()*/);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
82
20210129/zzx-20210129作业/json/LineConverter.cs
Normal file
82
20210129/zzx-20210129作业/json/LineConverter.cs
Normal file
@ -0,0 +1,82 @@
|
||||
/************************************************************************
|
||||
* ClassName : LineConverter
|
||||
* Description :
|
||||
* Author : Einsam
|
||||
* CreateTime : 2019/4/1 17:34:22
|
||||
************************************************************************
|
||||
* █████▒█ ██ ▄████▄ ██ ▄█▀ ██████╗ ██╗ ██╗ ██████╗
|
||||
* ▓██ ▒ ██ ▓██▒▒██▀ ▀█ ██▄█▒ ██╔══██╗██║ ██║██╔════╝
|
||||
* ▒████ ░▓██ ▒██░▒▓█ ▄ ▓███▄░ ██████╔╝██║ ██║██║ ███╗
|
||||
* ░▓█▒ ░▓▓█ ░██░▒▓▓▄ ▄██▒▓██ █▄ ██╔══██╗██║ ██║██║ ██║
|
||||
* ░▒█░ ▒▒█████▓ ▒ ▓███▀ ░▒██▒ █▄ ██████╔╝╚██████╔╝╚██████╔╝
|
||||
* ▒ ░ ░▒▓▒ ▒ ▒ ░ ░▒ ▒ ░▒ ▒▒ ▓▒ ╚═════╝ ╚═════╝ ╚═════╝
|
||||
* ░ ░░▒░ ░ ░ ░ ▒ ░ ░▒ ▒░
|
||||
* ░ ░ ░░░ ░ ░ ░ ░ ░░ ░
|
||||
* ░ ░ ░ ░ ░
|
||||
* ░
|
||||
************************************************************************
|
||||
* Copyright @ u-BIM Dev. 2018 . All rights reserved.
|
||||
************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Autodesk.Revit.DB;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace GraphicsStudy.Json
|
||||
{
|
||||
public class LineConverter : Newtonsoft.Json.JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNull();
|
||||
}
|
||||
else
|
||||
{
|
||||
JObject jObject = new JObject();
|
||||
jObject.Add("StartX", (value as Line).GetEndPoint(0).X);
|
||||
jObject.Add("StartY", (value as Line).GetEndPoint(0).Y);
|
||||
jObject.Add("StartZ", (value as Line).GetEndPoint(0).Z);
|
||||
jObject.Add("EndX", (value as Line).GetEndPoint(1).X);
|
||||
jObject.Add("EndY", (value as Line).GetEndPoint(1).Y);
|
||||
jObject.Add("EndZ", (value as Line).GetEndPoint(1).Z);
|
||||
writer.WriteValue(jObject.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
var jObject = JObject.Parse(reader.Value as string);
|
||||
|
||||
double startX = jObject.Value<double>("StartX");
|
||||
double startY = jObject.Value<double>("StartY");
|
||||
double startZ = jObject.Value<double>("StartZ");
|
||||
|
||||
XYZ startPoint = new XYZ(startX, startY, startZ);
|
||||
|
||||
double endX = jObject.Value<double>("EndX");
|
||||
double endY = jObject.Value<double>("EndY");
|
||||
double endZ = jObject.Value<double>("EndZ");
|
||||
|
||||
XYZ endPoint = new XYZ(endX, endY, endZ);
|
||||
|
||||
return Line.CreateBound(startPoint, endPoint);
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
if (objectType == typeof(Line))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
99
20210129/zzx-20210129作业/json/TransformConvert.cs
Normal file
99
20210129/zzx-20210129作业/json/TransformConvert.cs
Normal file
@ -0,0 +1,99 @@
|
||||
/************************************************************************
|
||||
* ClassName : JsonUtils
|
||||
* Description :
|
||||
* Author : Don
|
||||
* CreateTime : 2019/1/8 9:50:30
|
||||
************************************************************************
|
||||
* █████▒█ ██ ▄████▄ ██ ▄█▀ ██████╗ ██╗ ██╗ ██████╗
|
||||
* ▓██ ▒ ██ ▓██▒▒██▀ ▀█ ██▄█▒ ██╔══██╗██║ ██║██╔════╝
|
||||
* ▒████ ░▓██ ▒██░▒▓█ ▄ ▓███▄░ ██████╔╝██║ ██║██║ ███╗
|
||||
* ░▓█▒ ░▓▓█ ░██░▒▓▓▄ ▄██▒▓██ █▄ ██╔══██╗██║ ██║██║ ██║
|
||||
* ░▒█░ ▒▒█████▓ ▒ ▓███▀ ░▒██▒ █▄ ██████╔╝╚██████╔╝╚██████╔╝
|
||||
* ▒ ░ ░▒▓▒ ▒ ▒ ░ ░▒ ▒ ░▒ ▒▒ ▓▒ ╚═════╝ ╚═════╝ ╚═════╝
|
||||
* ░ ░░▒░ ░ ░ ░ ▒ ░ ░▒ ▒░
|
||||
* ░ ░ ░░░ ░ ░ ░ ░ ░░ ░
|
||||
* ░ ░ ░ ░ ░
|
||||
* ░
|
||||
************************************************************************
|
||||
* Copyright @ u-BIM Dev. 2018 . All rights reserved.
|
||||
************************************************************************/
|
||||
|
||||
using System;
|
||||
using Autodesk.Revit.DB;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace GraphicsStudy.Json
|
||||
{
|
||||
public class TransformConvert : Newtonsoft.Json.JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNull();
|
||||
}
|
||||
else
|
||||
{
|
||||
JObject jObject = new JObject();
|
||||
|
||||
jObject.Add("OriginX", (value as Transform).Origin.X);
|
||||
jObject.Add("OriginY", (value as Transform).Origin.Y);
|
||||
jObject.Add("OriginZ", (value as Transform).Origin.Z);
|
||||
|
||||
jObject.Add("BasicXx", (value as Transform).BasisX.X);
|
||||
jObject.Add("BasicXy", (value as Transform).BasisX.Y);
|
||||
jObject.Add("BasicXz", (value as Transform).BasisX.Z);
|
||||
|
||||
jObject.Add("BasicYx", (value as Transform).BasisY.X);
|
||||
jObject.Add("BasicYy", (value as Transform).BasisY.Y);
|
||||
jObject.Add("BasicYz", (value as Transform).BasisY.Z);
|
||||
|
||||
jObject.Add("BasicZx", (value as Transform).BasisZ.X);
|
||||
jObject.Add("BasicZy", (value as Transform).BasisZ.Y);
|
||||
jObject.Add("BasicZz", (value as Transform).BasisZ.Z);
|
||||
|
||||
writer.WriteValue(jObject.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
var jObject = JObject.Parse(reader.Value as string);
|
||||
|
||||
double ox = jObject.Value<double>("OriginX");
|
||||
double oy = jObject.Value<double>("OriginY");
|
||||
double oz = jObject.Value<double>("OriginZ");
|
||||
|
||||
double xx = jObject.Value<double>("BasicXx");
|
||||
double xy = jObject.Value<double>("BasicXy");
|
||||
double xz = jObject.Value<double>("BasicXz");
|
||||
|
||||
double yx = jObject.Value<double>("BasicYx");
|
||||
double yy = jObject.Value<double>("BasicYy");
|
||||
double yz = jObject.Value<double>("BasicYz");
|
||||
|
||||
double zx = jObject.Value<double>("BasicZx");
|
||||
double zy = jObject.Value<double>("BasicZy");
|
||||
double zz = jObject.Value<double>("BasicZz");
|
||||
|
||||
Transform transform = Transform.Identity;
|
||||
transform.Origin = new XYZ(ox, oy, oz);
|
||||
transform.BasisX = new XYZ(xx, xy, xz);
|
||||
transform.BasisY = new XYZ(yx, yy, yz);
|
||||
transform.BasisZ = new XYZ(zx, zy, zz);
|
||||
|
||||
return transform;
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
if (objectType == typeof(Transform))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
51
20210129/zzx-20210129作业/json/XYZConverter.cs
Normal file
51
20210129/zzx-20210129作业/json/XYZConverter.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using Autodesk.Revit.DB;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GraphicsStudy.Json
|
||||
{
|
||||
public class XYZConverter : Newtonsoft.Json.JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNull();
|
||||
}
|
||||
else
|
||||
{
|
||||
JObject jObject = new JObject();
|
||||
jObject.Add("X", (value as XYZ).X);
|
||||
jObject.Add("Y", (value as XYZ).Y);
|
||||
jObject.Add("Z", (value as XYZ).Z);
|
||||
writer.WriteValue(jObject.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
var jObject = JObject.Parse(reader.Value as string);
|
||||
|
||||
double x = jObject.Value<double>("X");
|
||||
double y = jObject.Value<double>("Y");
|
||||
double z = jObject.Value<double>("Z");
|
||||
|
||||
return new XYZ(x, y, z);
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
if (objectType == typeof(XYZ))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user